<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>SpringCloud Alibaba | 浪飞IT小栈</title>
    <meta name="generator" content="VuePress 1.9.9">
    <link rel="icon" href="/img/favicon.ico">
    <meta name="description" content="前后端技术分享">
    <meta name="keywords" content="前端博客,后端博客,人工智能,个人技术博客,前端,前端开发,前端框架,web前端,前端面试题,技术文档,学习,面试,JavaScript,js,ES6,TypeScript,vue,python,css3,html5,Node,git,github,markdown,java,Spring,Springboot,Redis,mysql">
    <meta name="baidu-site-verification" content="7F55weZDDc">
    <meta name="theme-color" content="#11a8cd">
    
    <link rel="preload" href="/assets/css/0.styles.ada71c49.css" as="style"><link rel="preload" href="/assets/js/app.a2a00aa5.js" as="script"><link rel="preload" href="/assets/js/2.256f807a.js" as="script"><link rel="preload" href="/assets/js/63.f849fe75.js" as="script"><link rel="prefetch" href="/assets/js/10.ab144fe3.js"><link rel="prefetch" href="/assets/js/11.062e0879.js"><link rel="prefetch" href="/assets/js/12.f9c60882.js"><link rel="prefetch" href="/assets/js/13.0a6ebfcf.js"><link rel="prefetch" href="/assets/js/14.03fc6f12.js"><link rel="prefetch" href="/assets/js/15.19dbf9fd.js"><link rel="prefetch" href="/assets/js/16.6e68160b.js"><link rel="prefetch" href="/assets/js/17.1d678834.js"><link rel="prefetch" href="/assets/js/18.4536fd5c.js"><link rel="prefetch" href="/assets/js/19.9c1b65df.js"><link rel="prefetch" href="/assets/js/20.c5f96e71.js"><link rel="prefetch" href="/assets/js/21.3f0b334e.js"><link rel="prefetch" href="/assets/js/22.55c868be.js"><link rel="prefetch" href="/assets/js/23.91286241.js"><link rel="prefetch" href="/assets/js/24.c1af1909.js"><link rel="prefetch" href="/assets/js/25.a995a755.js"><link rel="prefetch" href="/assets/js/26.abc5dea3.js"><link rel="prefetch" href="/assets/js/27.1eb62850.js"><link rel="prefetch" href="/assets/js/28.b383a871.js"><link rel="prefetch" href="/assets/js/29.088e0d73.js"><link rel="prefetch" href="/assets/js/3.cd716564.js"><link rel="prefetch" href="/assets/js/30.3ccc98d5.js"><link rel="prefetch" href="/assets/js/31.aab7cc23.js"><link rel="prefetch" href="/assets/js/32.5e16b7e6.js"><link rel="prefetch" href="/assets/js/33.4aaeb741.js"><link rel="prefetch" href="/assets/js/34.45e8e898.js"><link rel="prefetch" href="/assets/js/35.5899c97a.js"><link rel="prefetch" href="/assets/js/36.32e2e263.js"><link rel="prefetch" href="/assets/js/37.c4e27bbd.js"><link rel="prefetch" href="/assets/js/38.fe5e60d2.js"><link rel="prefetch" href="/assets/js/39.2cb0b4ee.js"><link rel="prefetch" href="/assets/js/4.edf4d5e7.js"><link rel="prefetch" href="/assets/js/40.08d5bead.js"><link rel="prefetch" href="/assets/js/41.2f80f24b.js"><link rel="prefetch" href="/assets/js/42.43a49913.js"><link rel="prefetch" href="/assets/js/43.08da7ec2.js"><link rel="prefetch" href="/assets/js/44.7c7cb8a9.js"><link rel="prefetch" href="/assets/js/45.0ddf7e7f.js"><link rel="prefetch" href="/assets/js/46.7c4609a5.js"><link rel="prefetch" href="/assets/js/47.edf3ddf8.js"><link rel="prefetch" href="/assets/js/48.569a90b9.js"><link rel="prefetch" href="/assets/js/49.fa368ac8.js"><link rel="prefetch" href="/assets/js/5.24054156.js"><link rel="prefetch" href="/assets/js/50.b9b3ea7f.js"><link rel="prefetch" href="/assets/js/51.a2ebf0fe.js"><link rel="prefetch" href="/assets/js/52.31f02238.js"><link rel="prefetch" href="/assets/js/53.4110ef1e.js"><link rel="prefetch" href="/assets/js/54.dab741c7.js"><link rel="prefetch" href="/assets/js/55.f27984e9.js"><link rel="prefetch" href="/assets/js/56.bd4c1a8e.js"><link rel="prefetch" href="/assets/js/57.375d40cb.js"><link rel="prefetch" href="/assets/js/58.932e3864.js"><link rel="prefetch" href="/assets/js/59.e657be1b.js"><link rel="prefetch" href="/assets/js/6.b259c061.js"><link rel="prefetch" href="/assets/js/60.88d94274.js"><link rel="prefetch" href="/assets/js/61.fa71d884.js"><link rel="prefetch" href="/assets/js/62.a9d3b3b7.js"><link rel="prefetch" href="/assets/js/64.1770f7a9.js"><link rel="prefetch" href="/assets/js/65.6afc33db.js"><link rel="prefetch" href="/assets/js/66.799f2eb4.js"><link rel="prefetch" href="/assets/js/67.f36b2f7d.js"><link rel="prefetch" href="/assets/js/68.868cd00b.js"><link rel="prefetch" href="/assets/js/69.4c29b907.js"><link rel="prefetch" href="/assets/js/7.e34ea00f.js"><link rel="prefetch" href="/assets/js/70.1b993ed4.js"><link rel="prefetch" href="/assets/js/71.e16ad4f9.js"><link rel="prefetch" href="/assets/js/72.627505e6.js"><link rel="prefetch" href="/assets/js/73.32768e14.js"><link rel="prefetch" href="/assets/js/74.968a580d.js"><link rel="prefetch" href="/assets/js/75.42d5af3d.js"><link rel="prefetch" href="/assets/js/76.19a69d31.js"><link rel="prefetch" href="/assets/js/77.cf627a78.js"><link rel="prefetch" href="/assets/js/78.07177880.js"><link rel="prefetch" href="/assets/js/8.c4dd00ce.js"><link rel="prefetch" href="/assets/js/9.0ff73be2.js">
    <link rel="stylesheet" href="/assets/css/0.styles.ada71c49.css">
  </head>
  <body class="theme-mode-light">
    <div id="app" data-server-rendered="true"><div class="theme-container sidebar-open have-rightmenu"><header class="navbar blur"><div title="目录" class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="/img/logo.png" alt="浪飞IT小栈" class="logo"> <span class="site-name can-hide">浪飞IT小栈</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/" class="nav-link">首页</a></div><div class="nav-item"><a href="/pages/6138ae/" class="nav-link">Java速通</a></div><div class="nav-item"><a href="/pages/705b35/" class="nav-link">面试经典</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="精品项目" class="dropdown-title"><a href="/project/" class="link-title">精品项目</a> <span class="title" style="display:none;">精品项目</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>桌面端</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/pages/8143cc480faf9a11/" class="nav-link">JavaScript</a></li></ul></li><li class="dropdown-item"><h4>PC后端</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/note/javascript/" class="nav-link">项目1</a></li><li class="dropdown-subitem"><a href="/note/typescript-axios/" class="nav-link">项目二</a></li></ul></li></ul></div></div><div class="nav-item"><a href="/about/" class="nav-link">电子书</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="索引" class="dropdown-title"><a href="/archives/" class="link-title">索引</a> <span class="title" style="display:none;">索引</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/" class="nav-link">分类</a></li><li class="dropdown-item"><!----> <a href="/tags/" class="nav-link">标签</a></li><li class="dropdown-item"><!----> <a href="/archives/" class="nav-link">归档</a></li></ul></div></div><div class="nav-item"><a href="/pages/0c07b2/" class="nav-link">IT杂货铺</a></div><div class="nav-item"><a href="/archives/" class="nav-link">更多资源</a></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <div class="sidebar-hover-trigger"></div> <aside class="sidebar" style="display:none;"><div class="blogger"><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/avatar.jpg"> <div class="blogger-info"><h3>浪飞yes</h3> <span>无法简单的人儿~</span></div></div> <nav class="nav-links"><div class="nav-item"><a href="/" class="nav-link">首页</a></div><div class="nav-item"><a href="/pages/6138ae/" class="nav-link">Java速通</a></div><div class="nav-item"><a href="/pages/705b35/" class="nav-link">面试经典</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="精品项目" class="dropdown-title"><a href="/project/" class="link-title">精品项目</a> <span class="title" style="display:none;">精品项目</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>桌面端</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/pages/8143cc480faf9a11/" class="nav-link">JavaScript</a></li></ul></li><li class="dropdown-item"><h4>PC后端</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/note/javascript/" class="nav-link">项目1</a></li><li class="dropdown-subitem"><a href="/note/typescript-axios/" class="nav-link">项目二</a></li></ul></li></ul></div></div><div class="nav-item"><a href="/about/" class="nav-link">电子书</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="索引" class="dropdown-title"><a href="/archives/" class="link-title">索引</a> <span class="title" style="display:none;">索引</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/" class="nav-link">分类</a></li><li class="dropdown-item"><!----> <a href="/tags/" class="nav-link">标签</a></li><li class="dropdown-item"><!----> <a href="/archives/" class="nav-link">归档</a></li></ul></div></div><div class="nav-item"><a href="/pages/0c07b2/" class="nav-link">IT杂货铺</a></div><div class="nav-item"><a href="/archives/" class="nav-link">更多资源</a></div> <!----></nav>  <ul class="sidebar-links"><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>开篇</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>基础语法</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>面向对象</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>常用类</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>基础高级</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>数据库</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>前端技术</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>热门框架</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>前后端分离</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>权限控制</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>NoSQL</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>微服务</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/pages/2ea428/" aria-current="page" class="active sidebar-link">SpringCloud Alibaba</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level2"><a href="/pages/2ea428/#一、今日学习内容与目标" class="sidebar-link">一、今日学习内容与目标</a></li><li class="sidebar-sub-header level2"><a href="/pages/2ea428/#二、微服务概念" class="sidebar-link">二、微服务概念</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/2ea428/#_2-1-课前科普一些术语" class="sidebar-link">2.1 课前科普一些术语</a></li><li class="sidebar-sub-header level3"><a href="/pages/2ea428/#_2-2-单体、分布式、集群" class="sidebar-link">2.2 单体、分布式、集群</a></li><li class="sidebar-sub-header level3"><a href="/pages/2ea428/#_2-3-系统架构演变" class="sidebar-link">2.3 系统架构演变</a></li><li class="sidebar-sub-header level4"><a href="/pages/2ea428/#_2-3-1-单体应用架构" class="sidebar-link">2.3.1 单体应用架构</a></li><li class="sidebar-sub-header level4"><a href="/pages/2ea428/#_2-3-2-垂直应用架构" class="sidebar-link">2.3.2 垂直应用架构</a></li><li class="sidebar-sub-header level4"><a href="/pages/2ea428/#_2-3-3-分布式架构" class="sidebar-link">2.3.3 分布式架构</a></li><li class="sidebar-sub-header level4"><a href="/pages/2ea428/#_2-3-4-soa架构" class="sidebar-link">2.3.4 SOA架构</a></li><li class="sidebar-sub-header level4"><a href="/pages/2ea428/#_2-3-5-微服务架构" class="sidebar-link">2.3.5 微服务架构</a></li><li class="sidebar-sub-header level3"><a href="/pages/2ea428/#_2-4-微服务架构介绍" class="sidebar-link">2.4 微服务架构介绍</a></li><li class="sidebar-sub-header level3"><a href="/pages/2ea428/#_2-5-springcloud介绍" class="sidebar-link">2.5 SpringCloud介绍</a></li><li class="sidebar-sub-header level4"><a href="/pages/2ea428/#_2-5-1-springboot和springcloud有啥关系" class="sidebar-link">2.5.1 SpringBoot和SpringCloud有啥关系?</a></li><li class="sidebar-sub-header level4"><a href="/pages/2ea428/#_2-5-2-springcloud版本名称" class="sidebar-link">2.5.2 SpringCloud版本名称?</a></li><li class="sidebar-sub-header level4"><a href="/pages/2ea428/#_2-5-3-为什么是springcloud" class="sidebar-link">2.5.3 为什么是SpringCloud ？</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/2ea428/#三、微服务环境搭建" class="sidebar-link">三、微服务环境搭建</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/2ea428/#_3-1-一些说明" class="sidebar-link">3.1 一些说明</a></li><li class="sidebar-sub-header level3"><a href="/pages/2ea428/#_3-2-技术选型" class="sidebar-link">3.2 技术选型</a></li><li class="sidebar-sub-header level3"><a href="/pages/2ea428/#_3-3-模块设计" class="sidebar-link">3.3 模块设计</a></li><li class="sidebar-sub-header level3"><a href="/pages/2ea428/#_3-4-版本说明" class="sidebar-link">3.4 版本说明</a></li><li class="sidebar-sub-header level3"><a href="/pages/2ea428/#_3-5-创建父工程" class="sidebar-link">3.5 创建父工程</a></li><li class="sidebar-sub-header level3"><a href="/pages/2ea428/#_3-6-创建商品微服务" class="sidebar-link">3.6 创建商品微服务</a></li><li class="sidebar-sub-header level3"><a href="/pages/2ea428/#_3-7-创建订单微服务" class="sidebar-link">3.7 创建订单微服务</a></li><li class="sidebar-sub-header level3"><a href="/pages/2ea428/#_3-8-服务间如何进行远程调用" class="sidebar-link">3.8 服务间如何进行远程调用</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/2ea428/#四、服务治理-nacos-discovery" class="sidebar-link">四、服务治理  Nacos Discovery</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/2ea428/#_4-1-什么是服务治理" class="sidebar-link">4.1 什么是服务治理</a></li><li class="sidebar-sub-header level3"><a href="/pages/2ea428/#_4-2-常见注册中心" class="sidebar-link">4.2 常见注册中心</a></li><li class="sidebar-sub-header level3"><a href="/pages/2ea428/#_4-3-nacos-简介" class="sidebar-link">4.3 Nacos 简介</a></li><li class="sidebar-sub-header level3"><a href="/pages/2ea428/#_4-4-nacos实战入门" class="sidebar-link">4.4 Nacos实战入门</a></li><li class="sidebar-sub-header level4"><a href="/pages/2ea428/#_4-4-1-搭建nacos环境" class="sidebar-link">4.4.1 搭建Nacos环境</a></li><li class="sidebar-sub-header level4"><a href="/pages/2ea428/#_4-4-2-将商品服务注册到nacos" class="sidebar-link">4.4.2 将商品服务注册到Nacos</a></li><li class="sidebar-sub-header level4"><a href="/pages/2ea428/#_4-4-3-将订单服务注册到nacos" class="sidebar-link">4.4.3 将订单服务注册到Nacos</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/2ea428/#五、远程调用负载均衡-ribbon" class="sidebar-link">五、远程调用负载均衡 Ribbon</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/2ea428/#_5-1-背景铺垫" class="sidebar-link">5.1 背景铺垫</a></li><li class="sidebar-sub-header level3"><a href="/pages/2ea428/#_5-2-什么是负载均衡" class="sidebar-link">5.2 什么是负载均衡</a></li><li class="sidebar-sub-header level3"><a href="/pages/2ea428/#_5-3-自定义负载均衡" class="sidebar-link">5.3 自定义负载均衡</a></li><li class="sidebar-sub-header level3"><a href="/pages/2ea428/#_5-4-基于ribbon实现负载均衡" class="sidebar-link">5.4 基于Ribbon实现负载均衡</a></li><li class="sidebar-sub-header level3"><a href="/pages/2ea428/#_5-5-ribbon支持的负载均衡策略" class="sidebar-link">5.5 Ribbon支持的负载均衡策略</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/2ea428/#六、远程调用feign" class="sidebar-link">六、远程调用Feign</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/2ea428/#_6-1-什么是feign" class="sidebar-link">6.1 什么是Feign</a></li><li class="sidebar-sub-header level3"><a href="/pages/2ea428/#_6-2-订单微服务集成feign" class="sidebar-link">6.2 订单微服务集成Feign</a></li><li class="sidebar-sub-header level3"><a href="/pages/2ea428/#_6-3-feign接口使用原理" class="sidebar-link">6.3 Feign接口使用原理</a></li><li class="sidebar-sub-header level3"><a href="/pages/2ea428/#_6-4-feign接口定义注意要点" class="sidebar-link">6.4 Feign接口定义注意要点</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/2ea428/#七、服务熔断降级-sentinel" class="sidebar-link">七、服务熔断降级 Sentinel</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/2ea428/#_7-1-高并发带来的问题" class="sidebar-link">7.1 高并发带来的问题</a></li><li class="sidebar-sub-header level3"><a href="/pages/2ea428/#_6-2-服务器雪崩效应" class="sidebar-link">6.2 服务器雪崩效应</a></li><li class="sidebar-sub-header level3"><a href="/pages/2ea428/#_7-2-常见容错方案" class="sidebar-link">7.2 常见容错方案</a></li><li class="sidebar-sub-header level3"><a href="/pages/2ea428/#_7-3-常见的容错组件" class="sidebar-link">7.3 常见的容错组件</a></li><li class="sidebar-sub-header level3"><a href="/pages/2ea428/#_7-4-sentinel入门" class="sidebar-link">7.4 Sentinel入门</a></li><li class="sidebar-sub-header level4"><a href="/pages/2ea428/#_7-4-1-什么是sentinel" class="sidebar-link">7.4.1 什么是Sentinel</a></li><li class="sidebar-sub-header level4"><a href="/pages/2ea428/#_7-4-2-订单微服务集成sentinel" class="sidebar-link">7.4.2 订单微服务集成Sentinel</a></li><li class="sidebar-sub-header level4"><a href="/pages/2ea428/#_7-4-3-安装sentinel控制台" class="sidebar-link">7.4.3 安装Sentinel控制台</a></li><li class="sidebar-sub-header level4"><a href="/pages/2ea428/#_7-4-4-实现一个接口的限流" class="sidebar-link">7.4.4 实现一个接口的限流</a></li><li class="sidebar-sub-header level4"><a href="/pages/2ea428/#_7-4-5-sentinel容错的维度" class="sidebar-link">7.4.5 Sentinel容错的维度</a></li><li class="sidebar-sub-header level4"><a href="/pages/2ea428/#_7-4-6-sentinel规则种类" class="sidebar-link">7.4.6 Sentinel规则种类</a></li><li class="sidebar-sub-header level4"><a href="/pages/2ea428/#_7-4-7-sentinel控制实现原理" class="sidebar-link">7.4.7 Sentinel控制实现原理</a></li><li class="sidebar-sub-header level3"><a href="/pages/2ea428/#_7-5-sentinel规则-流控" class="sidebar-link">7.5 Sentinel规则-流控</a></li><li class="sidebar-sub-header level4"><a href="/pages/2ea428/#_7-5-1-流控规则" class="sidebar-link">7.5.1 流控规则</a></li><li class="sidebar-sub-header level5"><a href="/pages/2ea428/#_7-5-1-1-qps流控" class="sidebar-link">7.5.1.1 QPS流控</a></li><li class="sidebar-sub-header level5"><a href="/pages/2ea428/#_7-5-1-2-线程数流控" class="sidebar-link">7.5.1.2 线程数流控</a></li><li class="sidebar-sub-header level4"><a href="/pages/2ea428/#_7-5-2-流控模式" class="sidebar-link">7.5.2 流控模式</a></li><li class="sidebar-sub-header level5"><a href="/pages/2ea428/#_7-5-2-1-直接流控模式" class="sidebar-link">7.5.2.1 直接流控模式</a></li><li class="sidebar-sub-header level5"><a href="/pages/2ea428/#_7-5-2-2-关联流控模式" class="sidebar-link">7.5.2.2 关联流控模式</a></li><li class="sidebar-sub-header level5"><a href="/pages/2ea428/#_7-5-2-3-链路流控模式" class="sidebar-link">7.5.2.3 链路流控模式</a></li><li class="sidebar-sub-header level4"><a href="/pages/2ea428/#_7-5-3-流控效果" class="sidebar-link">7.5.3 流控效果</a></li><li class="sidebar-sub-header level3"><a href="/pages/2ea428/#_7-6-sentinel规则-降级" class="sidebar-link">7.6 Sentinel规则-降级</a></li><li class="sidebar-sub-header level4"><a href="/pages/2ea428/#_7-6-1-慢调用比例案例" class="sidebar-link">7.6.1 慢调用比例案例</a></li><li class="sidebar-sub-header level4"><a href="/pages/2ea428/#_7-6-2-异常比例案例" class="sidebar-link">7.6.2 异常比例案例</a></li><li class="sidebar-sub-header level4"><a href="/pages/2ea428/#_7-6-3-异常数案例" class="sidebar-link">7.6.3 异常数案例</a></li><li class="sidebar-sub-header level3"><a href="/pages/2ea428/#_7-7-sentinel规则-热点" class="sidebar-link">7.7 Sentinel规则-热点</a></li><li class="sidebar-sub-header level3"><a href="/pages/2ea428/#_7-8-sentinel规则-授权" class="sidebar-link">7.8 Sentinel规则-授权</a></li><li class="sidebar-sub-header level3"><a href="/pages/2ea428/#_7-9-sentinel规则-系统规则" class="sidebar-link">7.9 Sentinel规则-系统规则</a></li><li class="sidebar-sub-header level3"><a href="/pages/2ea428/#_7-10-sentinel-自定义异常返回" class="sidebar-link">7.10  Sentinel 自定义异常返回</a></li><li class="sidebar-sub-header level3"><a href="/pages/2ea428/#_7-11-sentinelresource的使用" class="sidebar-link">7.11 @SentinelResource的使用</a></li><li class="sidebar-sub-header level3"><a href="/pages/2ea428/#_7-12-feign整合sentinel" class="sidebar-link">7.12 Feign整合Sentinel</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/2ea428/#八、服务网关gateway" class="sidebar-link">八、服务网关Gateway</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/2ea428/#_8-1-常见网关介绍" class="sidebar-link">8.1 常见网关介绍</a></li><li class="sidebar-sub-header level3"><a href="/pages/2ea428/#_8-2-gateway简介" class="sidebar-link">8.2 Gateway简介</a></li><li class="sidebar-sub-header level3"><a href="/pages/2ea428/#_8-3-gateway快速入门" class="sidebar-link">8.3 Gateway快速入门</a></li><li class="sidebar-sub-header level3"><a href="/pages/2ea428/#_8-4-自定义路由规则" class="sidebar-link">8.4 自定义路由规则</a></li><li class="sidebar-sub-header level3"><a href="/pages/2ea428/#_8-5-gateway概念" class="sidebar-link">8.5 Gateway概念</a></li><li class="sidebar-sub-header level3"><a href="/pages/2ea428/#_8-6-过滤器filter" class="sidebar-link">8.6 过滤器Filter</a></li><li class="sidebar-sub-header level4"><a href="/pages/2ea428/#_8-6-1-局部路由过滤器" class="sidebar-link">8.6.1 局部路由过滤器</a></li><li class="sidebar-sub-header level4"><a href="/pages/2ea428/#_8-6-2-全局路由过滤器" class="sidebar-link">8.6.2 全局路由过滤器</a></li><li class="sidebar-sub-header level3"><a href="/pages/2ea428/#_8-7-集成sentinel实现网关限流" class="sidebar-link">8.7 集成Sentinel实现网关限流</a></li><li class="sidebar-sub-header level4"><a href="/pages/2ea428/#_8-7-1-网关集成sentinel" class="sidebar-link">8.7.1 网关集成Sentinel</a></li><li class="sidebar-sub-header level4"><a href="/pages/2ea428/#_8-7-2-api分组" class="sidebar-link">8.7.2 API分组</a></li><li class="sidebar-sub-header level4"><a href="/pages/2ea428/#_8-7-3-修改限流默认返回格式" class="sidebar-link">8.7.3 修改限流默认返回格式</a></li><li class="sidebar-sub-header level3"><a href="/pages/2ea428/#_8-8-gateway总结" class="sidebar-link">8.8 Gateway总结</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/2ea428/#九、链路追踪-sleuth-zipkin" class="sidebar-link">九、链路追踪 Sleuth&amp;Zipkin</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/2ea428/#_9-1-常见的链路追踪技术" class="sidebar-link">9.1 常见的链路追踪技术</a></li><li class="sidebar-sub-header level3"><a href="/pages/2ea428/#_9-2-集成链路追踪组件sleuth" class="sidebar-link">9.2 集成链路追踪组件Sleuth</a></li><li class="sidebar-sub-header level3"><a href="/pages/2ea428/#_9-3-日志参数解释" class="sidebar-link">9.3 日志参数解释</a></li><li class="sidebar-sub-header level3"><a href="/pages/2ea428/#_9-4-zipkin-sleuth整合" class="sidebar-link">9.4 Zipkin+Sleuth整合</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/2ea428/#十、配置中心-nacos-config" class="sidebar-link">十、配置中心 Nacos Config</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/2ea428/#_10-1-服务配置中心介绍" class="sidebar-link">10.1 服务配置中心介绍</a></li><li class="sidebar-sub-header level3"><a href="/pages/2ea428/#_10-2-常见的服务配置中心" class="sidebar-link">10.2 常见的服务配置中心</a></li><li class="sidebar-sub-header level3"><a href="/pages/2ea428/#_10-3-nacos-config入门" class="sidebar-link">10.3 Nacos Config入门</a></li><li class="sidebar-sub-header level3"><a href="/pages/2ea428/#_10-4-配置动态刷新" class="sidebar-link">10.4 配置动态刷新</a></li><li class="sidebar-sub-header level3"><a href="/pages/2ea428/#_10-5-配置共享" class="sidebar-link">10.5 配置共享</a></li><li class="sidebar-sub-header level4"><a href="/pages/2ea428/#_10-5-1-配置环境" class="sidebar-link">10.5.1 配置环境</a></li><li class="sidebar-sub-header level4"><a href="/pages/2ea428/#_10-5-2-环境配置文件共享" class="sidebar-link">10.5.2 环境配置文件共享</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/2ea428/#十一、小结与作业" class="sidebar-link">十一、小结与作业</a></li></ul></li><li><a href="/pages/0a481a/" class="sidebar-link">分布式事务与Seata落地</a></li></ul></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>消息中间件</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>脚手架</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>技术增值</span> <span class="arrow right"></span></p> <!----></section></li></ul> </aside> <div><main class="page"><div class="theme-vdoing-wrapper "><div class="articleInfo-wrap" data-v-06225672><div class="articleInfo" data-v-06225672><ul class="breadcrumbs" data-v-06225672><li data-v-06225672><a href="/" title="首页" class="iconfont icon-home router-link-active" data-v-06225672></a></li> <li data-v-06225672><a href="/categories/?category=Java%E9%80%9F%E9%80%9A" title="分类" data-v-06225672>Java速通</a></li><li data-v-06225672><a href="/categories/?category=%E5%BE%AE%E6%9C%8D%E5%8A%A1" title="分类" data-v-06225672>微服务</a></li></ul> <div class="info" data-v-06225672><div title="作者" class="author iconfont icon-touxiang" data-v-06225672><a href="https://github.com/langfeiyes" target="_blank" title="作者" class="beLink" data-v-06225672>langfeiyes</a></div> <div title="创建时间" class="date iconfont icon-riqi" data-v-06225672><a href="javascript:;" data-v-06225672>2024-03-12</a></div> <!----></div></div></div> <!----> <div class="content-wrapper"><div class="right-menu-wrapper"><div class="right-menu-margin"><div class="right-menu-title">目录</div> <div class="right-menu-content"></div></div></div> <h1><img src="">SpringCloud Alibaba<!----></h1>  <div class="theme-vdoing-content content__default"><h1 id="springcloud-alibaba"><a href="#springcloud-alibaba" class="header-anchor">#</a> SpringCloud Alibaba</h1> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20240312170938861.png" alt="image-20240312170938861"></p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/1.jpg" alt="1"></p> <h2 id="一、今日学习内容与目标"><a href="#一、今日学习内容与目标" class="header-anchor">#</a> 一、今日学习内容与目标</h2> <ul><li>了解微服务常用的概念</li> <li>了解项目架构演变</li> <li>掌握微服务环境搭建</li> <li>掌握微服务治理组件-Nacos Discovery</li> <li>掌握微服务负载均衡调度组件-Ribbon</li> <li>掌握微服务远程调度组件-Feign</li> <li>掌握微服务流控容错组件-Sentinel</li> <li>掌握微服务网关组件-Gateway</li> <li>掌握微服务链路追踪组件-Sleuth&amp;Zipkin</li> <li>掌握微服务配置中心-Nacos Config</li> <li>开口表述上面所有组件概念、定位、原理与运用(每个10分钟)</li></ul> <h2 id="二、微服务概念"><a href="#二、微服务概念" class="header-anchor">#</a> 二、微服务概念</h2> <h3 id="_2-1-课前科普一些术语"><a href="#_2-1-课前科普一些术语" class="header-anchor">#</a> 2.1 课前科普一些术语</h3> <p>上课前，科普一下项目开发过程中常出现的术语，方便后续课程内容的理解。</p> <p>**服务器：**分软件与硬件，软件：类型tomcat这种跑项目的程序， 硬件：用来部署项目的电脑(一般性能比个人电脑好)</p> <p>**服务：**操作系统上术语：一个程序，开发中术语：一个能对外提供功能的程序</p> <p>**微服务：**小的服务，一个完整项目可以拆n个子项目，这些子项目能独立运行，独立对为提供功能。</p> <p>**节点：**微观上：一个服务，宏观上：一台服务器</p> <p>**垂直扩展：**垂直扩展是指增强单机硬件性能</p> <p>**水平扩展：**通过增加更多的服务器或者程序实例来分散负载，从而提升存储能力和计算能力。</p> <p>**容错率：**允许服务器集群(一堆服务器)错误(异常/故障)出现的范围和概率，</p> <p>**高内聚低耦合：**内聚--&gt;讲究程序功能独立   耦合---&gt;讲究程序间交互，</p> <p>​       以java为例子：高内聚低耦合：讲究类设计时尽量简单(边界清晰/功能简单)，类与类间交互尽可能少(减少类间的相互调用)</p> <p>**流量：**有很多种说，开发中说的是访问量(请求次数)</p> <p>**服务间依赖：**项目与项目间的调用，程序与程序间的调用</p> <p>**资源调度：**各种资源进行合理有效的调节和测量及分析和使用，开发中资源：服务器，内存，CPU，IO等项目运行需要各种软硬件。</p> <p>**单点：**唯一，开发中的单点：唯一一个mysql数据库，唯一个服务器</p> <p>**单点故障：**如果项目/程序部署唯一一个服务器，它挂了，那就玩完了</p> <p>**宕机：**服务器挂了</p> <h3 id="_2-2-单体、分布式、集群"><a href="#_2-2-单体、分布式、集群" class="header-anchor">#</a> 2.2 单体、分布式、集群</h3> <p>我们学习微服务之前,需要先理解单体、集群、分布式这些概念，这样会帮助我们在学习后面课程会更加容易些.</p> <p><strong>单体</strong></p> <p>一个系统业务量很小的时候所有的代码都放在一个项目中就好了，然后这个项目部署在一台服务器上就好了。整个项目所有的服务都由这台服务器提供。这就是单机结构。</p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20201027172014044.png" alt="image-20201027172014044"></p> <p>单体应用开发简单,部署测试简单.但是存在一些问题,比如:单点问题,单机处理能力有限,当你的业务增长到一定程度的时候，单机的硬件资源将无法满足你的业务需求。</p> <p><strong>分布式</strong></p> <p>由于整个系统运行需要使用到Tomcat和MySQL，单台服务器处理的能力有限,2G的内存需要分配给Tomcat和MySQL使用，，随着业务越来越复杂，请求越来越多. 内存越来越不够用了，所以这时候我们就需要进行分布式的部署.</p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20201027173909529.png" alt="image-20201027173909529"></p> <p>我们进行一个评论的请求，这个请求是需要依赖<strong>分布</strong>在两台不同的服务器的组件[Tomat和MySQL],才能完成的. 所以叫做分布式的系统.</p> <p><strong>集群</strong></p> <p>在上面的图解中其实是存在问题的，比如Tomcat存在单点故障问题，一旦Tomcat所在的服务器宕机不可用了，我们就无法提供服务了,所以针对单点故障问题，我们会使用集群来解决.那什么是集群模式呢?</p> <p>单机处理到达瓶颈的时候，你就把单机复制几份，这样就构成了一个“集群”。集群中每台服务器就叫做这个集群的一个“节点”，所有节点构成了一个集群。每个节点都提供相同的服务，那么这样系统的处理能力就相当于提升了好几倍（有几个节点就相当于提升了这么多倍）。</p> <p>但问题是用户的请求究竟由哪个节点来处理呢？最好能够让此时此刻负载较小的节点来处理，这样使得每个节点的压力都比较平均。要实现这个功能，就需要在所有节点之前增加一个“调度者”的角色，用户的所有请求都先交给它，然后它根据当前所有节点的负载情况，决定将这个请求交给哪个节点处理。这个“调度者”有个牛逼了名字——负载均衡服务器。</p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20201027182534219.png" alt="image-20201027182534219"></strong></p> <p>我们在上面的图中仅展示了Tomcat的集群，如果MySQL压力比较大的情况下，我们也是可以对MySQL进行集群的.</p> <h3 id="_2-3-系统架构演变"><a href="#_2-3-系统架构演变" class="header-anchor">#</a> 2.3 系统架构演变</h3> <p>​	随着互联网的发展，网站应用的规模也不断的扩大，进而导致系统架构也在不断的变化。</p> <p>从互联网早起到现在，系统架构大体经历了下面几个过程:</p> <p>​       <strong>单体应用架构---&gt;垂直应用架构---&gt;分布式架构---&gt;SOA架构---&gt;微服务架构。</strong></p> <p>接下来我们就来了解一下每种系统架构是什么样子的， 以及各有什么优缺点。</p> <h4 id="_2-3-1-单体应用架构"><a href="#_2-3-1-单体应用架构" class="header-anchor">#</a> 2.3.1 单体应用架构</h4> <p>​    互联网早期，一般的网站应用流量较小，只需一个应用，将所有功能代码都部署在一起就可以，这</p> <p>样可以减少开发、部署和维护的成本。</p> <p>​    比如说一个电商系统，里面会包含很多用户管理，商品管理，订单管理，物流管理等等很多模块，</p> <p>我们会把它们做成一个web项目，然后部署到一台tomcat服务器上。</p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20201028094613926.png" alt="image-20201028094613926"></strong></p> <p><strong>优点：</strong></p> <ul><li><p>项目架构简单，小型项目的话， 开发成本低</p></li> <li><p>项目部署在一个节点上， 维护方便</p></li></ul> <p><strong>缺点：</strong></p> <ul><li><p>全部功能集成在一个工程中，对于大型项目来讲不易开发和维护</p></li> <li><p>项目模块之间紧密耦合，单点容错率低</p></li> <li><p>无法针对不同模块进行针对性优化和水平扩展</p></li></ul> <h4 id="_2-3-2-垂直应用架构"><a href="#_2-3-2-垂直应用架构" class="header-anchor">#</a> 2.3.2 垂直应用架构</h4> <p>​    随着访问量的逐渐增大，单一应用只能依靠增加节点来应对，但是这时候会发现并不是所有的模块</p> <p>都会有比较大的访问量.</p> <p>​    还是以上面的电商为例子， 用户访问量的增加可能影响的只是用户和订单模块， 但是对消息模块</p> <p>的影响就比较小. 那么此时我们希望只多增加几个订单模块， 而不增加消息模块. 此时单体应用就做不</p> <p>到了， 垂直应用就应运而生了.</p> <p>​    所谓的垂直应用架构，就是将原来的一个应用拆成互不相干的几个应用，以提升效率。比如我们可</p> <p>以将上面电商的单体应用拆分成:</p> <ul><li><p>电商系统(用户管理 商品管理 订单管理)</p></li> <li><p>后台系统(用户管理 订单管理 客户管理)</p></li> <li><p>CMS系统(广告管理 营销管理)</p></li></ul> <p>这样拆分完毕之后，一旦用户访问量变大，只需要增加电商系统的节点就可以了，而无需增加后台</p> <p>和CMS的节点。</p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20201028095743602.png" alt="image-20201028095743602"></strong></p> <p><strong>优点：</strong></p> <ul><li><p>系统拆分实现了流量分担，解决了并发问题，而且可以针对不同模块进行优化和水平扩展</p></li> <li><p>一个系统的问题不会影响到其他系统，提高容错率</p></li></ul> <p><strong>缺点：</strong></p> <ul><li><p>系统之间相互独立， 无法进行相互调用</p></li> <li><p>系统之间相互独立， 会有重复的开发任务</p></li></ul> <h4 id="_2-3-3-分布式架构"><a href="#_2-3-3-分布式架构" class="header-anchor">#</a> 2.3.3 分布式架构</h4> <p>​    当垂直应用越来越多，重复的业务代码就会越来越多。这时候，我们就思考可不可以将重复的代码</p> <p>抽取出来，做成统一的业务层作为独立的服务，然后由前端控制层调用不同的业务层服务呢？</p> <p>​    这就产生了新的分布式系统架构。它将把工程拆分成表现层和服务层两个部分，服务层中包含业务</p> <p>逻辑。表现层只需要处理和页面的交互，业务逻辑都是调用服务层的服务来实现。</p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20201028103739904.png" alt="image-20201028103739904"></p> <p><strong>优点</strong>：</p> <ul><li>抽取公共的功能为服务层，提高代码复用性</li></ul> <p><strong>缺点</strong>：</p> <ul><li>系统间耦合度变高，调用关系错综复杂，难以维护</li></ul> <h4 id="_2-3-4-soa架构"><a href="#_2-3-4-soa架构" class="header-anchor">#</a> 2.3.4 SOA架构</h4> <p>​    在分布式架构下，当服务越来越多，容量的评估，小服务资源的浪费等问题逐渐显现，此时需增加</p> <p>一个调度中心对集群进行实时管理。此时，用于资源调度和治理中心(SOA Service Oriented</p> <p>Architecture，面向服务的架构)是关键。</p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/SOA.jpg" alt="****"></p> <p><strong>优点</strong>:</p> <ul><li>使用注册中心解决了服务间调用关系的自动调节</li></ul> <p><strong>缺点</strong>:</p> <ul><li><p>服务间会有依赖关系，一旦某个环节出错会影响较大( 服务雪崩 )</p></li> <li><p>服务关系复杂，运维、测试部署困难</p></li></ul> <h4 id="_2-3-5-微服务架构"><a href="#_2-3-5-微服务架构" class="header-anchor">#</a> 2.3.5 微服务架构</h4> <p>微服务架构在某种程度上是面向服务的架构SOA继续发展的下一步，它更加强调服务的&quot;彻底拆分&quot;。</p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20201028110735067.png" alt="image-20201028110735067"></strong></p> <p><strong>优点</strong>：</p> <ul><li><p>服务原子化拆分，独立打包、部署和升级，保证每个微服务清晰的任务划分，利于扩展</p></li> <li><p>微服务之间采用RESTful等轻量级Http协议相互调用</p></li></ul> <p><strong>缺点</strong>：</p> <ul><li>分布式系统开发的技术成本高（容错、分布式事务等）</li></ul> <h3 id="_2-4-微服务架构介绍"><a href="#_2-4-微服务架构介绍" class="header-anchor">#</a> 2.4 微服务架构介绍</h3> <p>​    微服务架构， 简单的说就是将单体应用进一步拆分，拆分成更小的服务，每个服务都是一个可以独立运行的项目。</p> <p><strong>微服务架构的常见问题</strong></p> <p>一旦采用微服务系统架构，就势必会遇到这样几个问题：</p> <ul><li><p>这么多小服务，如何管理他们？</p></li> <li><p>这么多小服务，他们之间如何通讯？</p></li> <li><p>这么多小服务，客户端怎么访问他们？</p></li> <li><p>这么多小服务，一旦出现问题了，应该如何自处理？</p></li> <li><p>这么多小服务，一旦出现问题了，应该如何排错?</p></li></ul> <p>对于上面的问题，是任何一个微服务设计者都不能绕过去的，因此大部分的微服务产品都针对每一个问题提供了相应的组件来解决它们。</p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/miniservicestructure.jpg" alt="****"></p> <h3 id="_2-5-springcloud介绍"><a href="#_2-5-springcloud介绍" class="header-anchor">#</a> 2.5 SpringCloud介绍</h3> <p>Spring Cloud是一系列框架的集合。它利用Spring Boot的开发便利性巧妙地简化了分布式系统基础设施的开发，如服务发现注册、配置中心、消息总线、负载均衡、断路器、数据监控等，都可以用Spring Boot的开发风格做到一键启动和部署。</p> <p>Spring Cloud并没有重复制造轮子，它只是将目前各家公司开发的比较成熟、经得起实际考验的服务框架组合起来，通过Spring Boot风格进行再封装屏蔽掉了复杂的配置和实现原理，最终给开发者留出了一套简单易懂、易部署和易维护的分布式系统开发工具包。</p> <h4 id="_2-5-1-springboot和springcloud有啥关系"><a href="#_2-5-1-springboot和springcloud有啥关系" class="header-anchor">#</a> 2.5.1 SpringBoot和SpringCloud有啥关系?</h4> <ul><li><p>SpringBoot专注于快速方便的开发单个个体微服务。</p></li> <li><p>SpringCloud是关注全局的微服务协调整理治理框架，它将SpringBoot开发的一个个单体微服务整合并管理起来，为各个微服务之间提供，配置管理、服务发现、断路器、路由、事件总线、分布式事务、等等集成服务。</p></li></ul> <p><strong>总结</strong>: SpringBoot专注于快速、方便的开发单个微服务个体，SpringCloud关注全局的服务治理组件的集合。</p> <h4 id="_2-5-2-springcloud版本名称"><a href="#_2-5-2-springcloud版本名称" class="header-anchor">#</a> 2.5.2 SpringCloud版本名称?</h4> <p>因为Spring Cloud不同其他独立项目，它是拥有很多子项目的大项目。所以它是的版本是 版本名+版本号 （如Greenwich.SR6）。
版本名：是伦敦的地铁名
版本号：SR（Service Releases）是固定的 ,大概意思是稳定版本。后面会有一个递增的数字。
所以 Greenwich.SR6就是Greenwich的第6个Release版本。</p> <h4 id="_2-5-3-为什么是springcloud"><a href="#_2-5-3-为什么是springcloud" class="header-anchor">#</a> 2.5.3 为什么是SpringCloud ？</h4> <p>目前，业界比较成熟的服务框架有很多，比如：Hessian、CXF、Dubbo、Dubbox、Spring Cloud、gRPC、thrift等技术实现</p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/aa.jpg" alt="aa">*</p> <p>这里我们为什么选择SpringCloud Alibaba呢，主要因为SpringCloud Netflix的组件：服务注册与发现的 Eureka、服务限流降级的 Hystrix、网关 Zuul都已经停止更新了，当然继续使用是没问题的，只是出现问题，官方不维护，需要自行解决.</p> <h2 id="三、微服务环境搭建"><a href="#三、微服务环境搭建" class="header-anchor">#</a> 三、微服务环境搭建</h2> <h3 id="_3-1-一些说明"><a href="#_3-1-一些说明" class="header-anchor">#</a> 3.1 一些说明</h3> <p>为了方便讲解SpringCloud课程，我们以电商项目2个核心模块：商品模块、订单模块为例子，一一讲解SpringCloud组件的使用。</p> <p>学习SpringCloud组件要诀：<strong>不求甚解</strong></p> <p>1&gt;能解决啥问题</p> <p>2&gt;怎么解决(理解原理)</p> <p>3&gt;API调用(代码怎么写)--建议写3遍--【1遍抄全，2遍思考，3遍掌握】</p> <p>4&gt;总结，开口表述</p> <p><strong>5&gt;类比以前代码结构</strong></p> <p><strong>微服务----</strong>-完整项目按功能分类拆分成n个子项目/子模块，这些子模块能对外提供对应的功能。我们称这些服务为微服务</p> <p><strong>落地到代码：单看子项目，每个子项目就是一个完整项目(springmvc项目)</strong>----记住没啥高大上的</p> <p><strong>商品微服务</strong></p> <ul><li>对外提供查询商品列表接口</li> <li>对外提供查询某个商品信息接口</li></ul> <p><strong>订单微服务</strong></p> <ul><li>对外提供创建订单接口</li></ul> <p><strong>服务调用</strong></p> <p>在微服务架构中，最常见的场景就是微服务之间的相互调用。以下单为例子：客户向订单微服务发起一个下单的请求，在进行保存订单之前需要调用商品微服务查询商品的信息。</p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20201028144911439.png" alt="image-20201028144911439"></strong></p> <p>一般把调用方称为<strong>服务消费者</strong>，把被调用方称为<strong>服务提供者</strong>。</p> <p>上例中，订单微服务就是服务消费者， 而商品微服务是服务提供者。</p> <h3 id="_3-2-技术选型"><a href="#_3-2-技术选型" class="header-anchor">#</a> 3.2 技术选型</h3> <p>持久层:  MyBatis-Plus</p> <p>数据库: MySQL5.7</p> <p>其他: SpringCloud Alibaba 技术栈</p> <h3 id="_3-3-模块设计"><a href="#_3-3-模块设计" class="header-anchor">#</a> 3.3 模块设计</h3> <p>--- shop-parent 父工程</p> <p>​     --- shop-product-api 商品微服务api 【存放商品实体】</p> <p>​     --- shop-product-server 商品微服务 【端口:808x】</p> <p>​     --- shop-order-api 订单微服务api 【存放订单实体】</p> <p>​     --- shop-order-server 订单微服务 【端口:809x】</p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20221012162206756.png" alt="image-20221012162206756"></strong></p> <p>shop-product-server：子项目-商品微服务，对外提供查询商品信息的接口</p> <p>shop-order-server：子项目-订单微服务，对外提供创建订单的接口</p> <p>shop-product-api / shop-order-api : 各自微服务依赖的实体类，为啥要拆开？答案是：<strong>解耦</strong></p> <p>此处暂时先记住</p> <h3 id="_3-4-版本说明"><a href="#_3-4-版本说明" class="header-anchor">#</a> 3.4 版本说明</h3> <p>​	https://github.com/alibaba/spring-cloud-alibaba/wiki/版本说明</p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20201028150721156.png" alt="image-20201028150721156"></strong></p> <h3 id="_3-5-创建父工程"><a href="#_3-5-创建父工程" class="header-anchor">#</a> 3.5 创建父工程</h3> <p>创建 shop-parent 一个maven工程，然后在pom.xml文件中添加下面内容</p> <div class="language-xml line-numbers-mode"><pre class="language-xml"><code><span class="token prolog">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>project</span> <span class="token attr-name">xmlns</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>http://maven.apache.org/POM/4.0.0<span class="token punctuation">&quot;</span></span>
         <span class="token attr-name"><span class="token namespace">xmlns:</span>xsi</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>http://www.w3.org/2001/XMLSchema-instance<span class="token punctuation">&quot;</span></span>
         <span class="token attr-name"><span class="token namespace">xsi:</span>schemaLocation</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>modelVersion</span><span class="token punctuation">&gt;</span></span>4.0.0<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>modelVersion</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>cn.wolfcode<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>shop-parent<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">&gt;</span></span>1.0.0<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>packaging</span><span class="token punctuation">&gt;</span></span>pom<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>packaging</span><span class="token punctuation">&gt;</span></span>
    <span class="token comment">&lt;!--父工程--&gt;</span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>parent</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>org.springframework.boot<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>spring-boot-starter-parent<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">&gt;</span></span>2.3.2.RELEASE<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>parent</span><span class="token punctuation">&gt;</span></span>
    <span class="token comment">&lt;!--依赖版本的锁定--&gt;</span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>properties</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>java.version</span><span class="token punctuation">&gt;</span></span>1.8<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>java.version</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>project.build.sourceEncoding</span><span class="token punctuation">&gt;</span></span>UTF-8<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>project.build.sourceEncoding</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>project.reporting.outputEncoding</span><span class="token punctuation">&gt;</span></span>UTF-8<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>project.reporting.outputEncoding</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>spring-cloud.version</span><span class="token punctuation">&gt;</span></span>Hoxton.SR8<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>spring-cloud.version</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>spring-cloud-alibaba.version</span><span class="token punctuation">&gt;</span></span>2.2.3.RELEASE<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>spring-cloud-alibaba.version</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>properties</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependencyManagement</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependencies</span><span class="token punctuation">&gt;</span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">&gt;</span></span>
                <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>org.springframework.cloud<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
                <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>spring-cloud-dependencies<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
                <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">&gt;</span></span>${spring-cloud.version}<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">&gt;</span></span>
                <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>type</span><span class="token punctuation">&gt;</span></span>pom<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>type</span><span class="token punctuation">&gt;</span></span>
                <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>scope</span><span class="token punctuation">&gt;</span></span>import<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>scope</span><span class="token punctuation">&gt;</span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">&gt;</span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">&gt;</span></span>
                <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>com.alibaba.cloud<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
                <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>spring-cloud-alibaba-dependencies<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
                <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">&gt;</span></span>${spring-cloud-alibaba.version}<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">&gt;</span></span>
                <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>type</span><span class="token punctuation">&gt;</span></span>pom<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>type</span><span class="token punctuation">&gt;</span></span>
                <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>scope</span><span class="token punctuation">&gt;</span></span>import<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>scope</span><span class="token punctuation">&gt;</span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependencies</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependencyManagement</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>project</span><span class="token punctuation">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br></div></div><h3 id="_3-6-创建商品微服务"><a href="#_3-6-创建商品微服务" class="header-anchor">#</a> 3.6 创建商品微服务</h3> <p><strong>1.创建shop-product-api项目，然后在pom.xml文件中添加下面内容</strong></p> <div class="language-xml line-numbers-mode"><pre class="language-xml"><code><span class="token prolog">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>project</span> <span class="token attr-name">xmlns</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>http://maven.apache.org/POM/4.0.0<span class="token punctuation">&quot;</span></span>
         <span class="token attr-name"><span class="token namespace">xmlns:</span>xsi</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>http://www.w3.org/2001/XMLSchema-instance<span class="token punctuation">&quot;</span></span>
         <span class="token attr-name"><span class="token namespace">xsi:</span>schemaLocation</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>parent</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>shop-parent<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>cn.wolfcode<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">&gt;</span></span>1.0.0<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>parent</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>modelVersion</span><span class="token punctuation">&gt;</span></span>4.0.0<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>modelVersion</span><span class="token punctuation">&gt;</span></span>

    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>shop-product-api<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
     <span class="token comment">&lt;!--依赖--&gt;</span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependencies</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">&gt;</span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>com.baomidou<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>mybatis-plus-boot-starter<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">&gt;</span></span>3.4.0<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">&gt;</span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>org.projectlombok<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>lombok<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependencies</span><span class="token punctuation">&gt;</span></span>

<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>project</span><span class="token punctuation">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br></div></div><p><strong>2 创建实体类</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token comment">//商品</span>
<span class="token annotation punctuation">@Getter</span>
<span class="token annotation punctuation">@Setter</span>
<span class="token annotation punctuation">@ToString</span>
<span class="token annotation punctuation">@TableName</span><span class="token punctuation">(</span><span class="token string">&quot;t_product&quot;</span><span class="token punctuation">)</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Product</span> <span class="token keyword">implements</span> <span class="token class-name">Serializable</span> <span class="token punctuation">{</span>
    <span class="token annotation punctuation">@TableId</span><span class="token punctuation">(</span>type<span class="token operator">=</span> <span class="token class-name">IdType</span><span class="token punctuation">.</span><span class="token constant">AUTO</span><span class="token punctuation">)</span>
    <span class="token keyword">private</span> <span class="token class-name">Long</span> id<span class="token punctuation">;</span><span class="token comment">//主键</span>
    <span class="token keyword">private</span> <span class="token class-name">String</span> name<span class="token punctuation">;</span><span class="token comment">//商品名称</span>
    <span class="token keyword">private</span> <span class="token class-name">Double</span> price<span class="token punctuation">;</span><span class="token comment">//商品价格</span>
    <span class="token keyword">private</span> <span class="token class-name">Integer</span> stock<span class="token punctuation">;</span><span class="token comment">//库存</span>
<span class="token punctuation">}</span>

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br></div></div><p><strong>3.创建shop-product-server项目，然后在pom.xml文件中添加下面内容</strong></p> <div class="language-xml line-numbers-mode"><pre class="language-xml"><code><span class="token prolog">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>project</span> <span class="token attr-name">xmlns</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>http://maven.apache.org/POM/4.0.0<span class="token punctuation">&quot;</span></span>
         <span class="token attr-name"><span class="token namespace">xmlns:</span>xsi</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>http://www.w3.org/2001/XMLSchema-instance<span class="token punctuation">&quot;</span></span>
         <span class="token attr-name"><span class="token namespace">xsi:</span>schemaLocation</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>parent</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>shop-parent<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>cn.wolfcode<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">&gt;</span></span>1.0.0<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>parent</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>modelVersion</span><span class="token punctuation">&gt;</span></span>4.0.0<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>modelVersion</span><span class="token punctuation">&gt;</span></span>

    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>shop-product-server<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependencies</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">&gt;</span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>org.springframework.boot<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>spring-boot-starter-web<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">&gt;</span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>mysql<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>mysql-connector-java<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">&gt;</span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>com.alibaba<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>fastjson<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">&gt;</span></span>1.2.56<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">&gt;</span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>cn.wolfcode<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>shop-product-api<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">&gt;</span></span>1.0.0<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependencies</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>project</span><span class="token punctuation">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br></div></div><p><strong>4.编写配置文件application.yml</strong></p> <div class="language-yaml line-numbers-mode"><pre class="language-yaml"><code><span class="token key atrule">server</span><span class="token punctuation">:</span>
  <span class="token key atrule">port</span><span class="token punctuation">:</span> <span class="token number">8081</span>
<span class="token key atrule">spring</span><span class="token punctuation">:</span>
  <span class="token key atrule">application</span><span class="token punctuation">:</span>
    <span class="token key atrule">name</span><span class="token punctuation">:</span> product<span class="token punctuation">-</span>service
  <span class="token key atrule">datasource</span><span class="token punctuation">:</span>
    <span class="token key atrule">driver-class-name</span><span class="token punctuation">:</span> com.mysql.cj.jdbc.Driver
    <span class="token key atrule">url</span><span class="token punctuation">:</span> jdbc<span class="token punctuation">:</span>mysql<span class="token punctuation">:</span>///shop<span class="token punctuation">-</span>product<span class="token punctuation">?</span>serverTimezone=UTC<span class="token important">&amp;useUnicode=true&amp;characterEncoding=utf-8&amp;useSSL=true</span>
    <span class="token key atrule">username</span><span class="token punctuation">:</span> root
    <span class="token key atrule">password</span><span class="token punctuation">:</span> admin

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><p><strong>5.在数据库中创建shop-product的数据库</strong></p> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code><span class="token keyword">CREATE</span> <span class="token keyword">TABLE</span> <span class="token identifier"><span class="token punctuation">`</span>t_product<span class="token punctuation">`</span></span> <span class="token punctuation">(</span>
  <span class="token identifier"><span class="token punctuation">`</span>id<span class="token punctuation">`</span></span> <span class="token keyword">bigint</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">AUTO_INCREMENT</span> <span class="token keyword">COMMENT</span> <span class="token string">'主键'</span><span class="token punctuation">,</span>
  <span class="token identifier"><span class="token punctuation">`</span>name<span class="token punctuation">`</span></span> <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">255</span><span class="token punctuation">)</span> <span class="token keyword">DEFAULT</span> <span class="token boolean">NULL</span> <span class="token keyword">COMMENT</span> <span class="token string">'商品名称'</span><span class="token punctuation">,</span>
  <span class="token identifier"><span class="token punctuation">`</span>price<span class="token punctuation">`</span></span> <span class="token keyword">double</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">)</span> <span class="token keyword">DEFAULT</span> <span class="token boolean">NULL</span> <span class="token keyword">COMMENT</span> <span class="token string">'商品价格'</span><span class="token punctuation">,</span>
  <span class="token identifier"><span class="token punctuation">`</span>stock<span class="token punctuation">`</span></span> <span class="token keyword">int</span> <span class="token keyword">DEFAULT</span> <span class="token boolean">NULL</span> <span class="token keyword">COMMENT</span> <span class="token string">'库存'</span><span class="token punctuation">,</span>
  <span class="token keyword">PRIMARY</span> <span class="token keyword">KEY</span> <span class="token punctuation">(</span><span class="token identifier"><span class="token punctuation">`</span>id<span class="token punctuation">`</span></span><span class="token punctuation">)</span>
<span class="token punctuation">)</span> <span class="token keyword">ENGINE</span><span class="token operator">=</span><span class="token keyword">InnoDB</span> <span class="token keyword">DEFAULT</span> <span class="token keyword">CHARSET</span><span class="token operator">=</span>utf8mb3<span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><div class="language-sql line-numbers-mode"><pre class="language-sql"><code><span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> t_product <span class="token keyword">VALUE</span><span class="token punctuation">(</span><span class="token boolean">NULL</span><span class="token punctuation">,</span><span class="token string">'小米'</span><span class="token punctuation">,</span><span class="token string">'1000'</span><span class="token punctuation">,</span><span class="token string">'5000'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 
<span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> t_product <span class="token keyword">VALUE</span><span class="token punctuation">(</span><span class="token boolean">NULL</span><span class="token punctuation">,</span><span class="token string">'华为'</span><span class="token punctuation">,</span><span class="token string">'2000'</span><span class="token punctuation">,</span><span class="token string">'5000'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 
<span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> t_product <span class="token keyword">VALUE</span><span class="token punctuation">(</span><span class="token boolean">NULL</span><span class="token punctuation">,</span><span class="token string">'苹果'</span><span class="token punctuation">,</span><span class="token string">'3000'</span><span class="token punctuation">,</span><span class="token string">'5000'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 
<span class="token keyword">INSERT</span> <span class="token keyword">INTO</span> t_product <span class="token keyword">VALUE</span><span class="token punctuation">(</span><span class="token boolean">NULL</span><span class="token punctuation">,</span><span class="token string">'OPPO'</span><span class="token punctuation">,</span><span class="token string">'4000'</span><span class="token punctuation">,</span><span class="token string">'5000'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p><strong>6.创建ProductMapper</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">package</span> <span class="token namespace">cn<span class="token punctuation">.</span>wolfcode<span class="token punctuation">.</span>mapper</span><span class="token punctuation">;</span>

<span class="token keyword">import</span> <span class="token import"><span class="token namespace">cn<span class="token punctuation">.</span>wolfcode<span class="token punctuation">.</span>domain<span class="token punctuation">.</span></span><span class="token class-name">Product</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">com<span class="token punctuation">.</span>baomidou<span class="token punctuation">.</span>mybatisplus<span class="token punctuation">.</span>core<span class="token punctuation">.</span>mapper<span class="token punctuation">.</span></span><span class="token class-name">BaseMapper</span></span><span class="token punctuation">;</span>

<span class="token keyword">public</span> <span class="token keyword">interface</span> <span class="token class-name">ProductMapper</span> <span class="token keyword">extends</span> <span class="token class-name">BaseMapper</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Product</span><span class="token punctuation">&gt;</span></span> <span class="token punctuation">{</span>
<span class="token punctuation">}</span>

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p><strong>7.创建ProductService接口和实现类</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">interface</span> <span class="token class-name">IProductService</span> <span class="token keyword">extends</span> <span class="token class-name">IService</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Product</span><span class="token punctuation">&gt;</span></span> <span class="token punctuation">{</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@Service</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">ProductServiceImpl</span> <span class="token keyword">extends</span> <span class="token class-name">ServiceImpl</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">ProductMapper</span><span class="token punctuation">,</span> <span class="token class-name">Product</span><span class="token punctuation">&gt;</span></span> <span class="token keyword">implements</span> <span class="token class-name">IProductService</span> <span class="token punctuation">{</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p><strong>8.创建Controller</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">package</span> <span class="token namespace">cn<span class="token punctuation">.</span>wolfcode<span class="token punctuation">.</span>controller</span><span class="token punctuation">;</span>
<span class="token annotation punctuation">@RestController</span>
<span class="token annotation punctuation">@Slf4j</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">ProductController</span> <span class="token punctuation">{</span>
    <span class="token annotation punctuation">@Autowired</span>
    <span class="token keyword">private</span> <span class="token class-name">ProductService</span> productService<span class="token punctuation">;</span>
    <span class="token comment">//商品信息查询</span>
    <span class="token annotation punctuation">@RequestMapping</span><span class="token punctuation">(</span><span class="token string">&quot;/products/{pid}&quot;</span><span class="token punctuation">)</span>
    <span class="token keyword">public</span> <span class="token class-name">Product</span> <span class="token function">findByPid</span><span class="token punctuation">(</span><span class="token annotation punctuation">@PathVariable</span><span class="token punctuation">(</span><span class="token string">&quot;pid&quot;</span><span class="token punctuation">)</span> <span class="token class-name">Long</span> pid<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        log<span class="token punctuation">.</span><span class="token function">info</span><span class="token punctuation">(</span><span class="token string">&quot;接下来要进行{}号商品信息的查询&quot;</span><span class="token punctuation">,</span> pid<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">Product</span> product <span class="token operator">=</span> productService<span class="token punctuation">.</span><span class="token function">findByPid</span><span class="token punctuation">(</span>pid<span class="token punctuation">)</span><span class="token punctuation">;</span>
        log<span class="token punctuation">.</span><span class="token function">info</span><span class="token punctuation">(</span><span class="token string">&quot;商品信息查询成功,内容为{}&quot;</span><span class="token punctuation">,</span> <span class="token constant">JSON</span><span class="token punctuation">.</span><span class="token function">toJSONString</span><span class="token punctuation">(</span>product<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> product<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br></div></div><p><strong>9.编写启动类ProductServer.java</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">package</span> <span class="token namespace">cn<span class="token punctuation">.</span>wolfcode</span><span class="token punctuation">;</span>

<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>boot<span class="token punctuation">.</span></span><span class="token class-name">SpringApplication</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>boot<span class="token punctuation">.</span>autoconfigure<span class="token punctuation">.</span></span><span class="token class-name">SpringBootApplication</span></span><span class="token punctuation">;</span>

<span class="token annotation punctuation">@SpringBootApplication</span>
<span class="token annotation punctuation">@MapperScan</span><span class="token punctuation">(</span><span class="token string">&quot;cn.wolfcode.mapper&quot;</span><span class="token punctuation">)</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">ProductServer</span> <span class="token punctuation">{</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">SpringApplication</span><span class="token punctuation">.</span><span class="token function">run</span><span class="token punctuation">(</span><span class="token class-name">ProductServer</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">,</span>args<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><p><strong>10.通过浏览器访问服务</strong></p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20221012173128164.png" alt="image-20221012173128164"></strong></p> <h3 id="_3-7-创建订单微服务"><a href="#_3-7-创建订单微服务" class="header-anchor">#</a> 3.7 创建订单微服务</h3> <p>1.创建shop-order-api项目，然后在pom.xml文件中添加下面内容</p> <div class="language-xml line-numbers-mode"><pre class="language-xml"><code><span class="token prolog">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>project</span> <span class="token attr-name">xmlns</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>http://maven.apache.org/POM/4.0.0<span class="token punctuation">&quot;</span></span>
         <span class="token attr-name"><span class="token namespace">xmlns:</span>xsi</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>http://www.w3.org/2001/XMLSchema-instance<span class="token punctuation">&quot;</span></span>
         <span class="token attr-name"><span class="token namespace">xsi:</span>schemaLocation</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>parent</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>shop-parent<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>cn.wolfcode<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">&gt;</span></span>1.0.0<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>parent</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>modelVersion</span><span class="token punctuation">&gt;</span></span>4.0.0<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>modelVersion</span><span class="token punctuation">&gt;</span></span>

    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>shop-order-api<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
 	<span class="token comment">&lt;!--依赖--&gt;</span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependencies</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">&gt;</span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>com.baomidou<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>mybatis-plus-boot-starter<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">&gt;</span></span>3.4.0<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">&gt;</span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>org.projectlombok<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>lombok<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependencies</span><span class="token punctuation">&gt;</span></span>

<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>project</span><span class="token punctuation">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br></div></div><p><strong>2 创建实体类</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">package</span> <span class="token namespace">cn<span class="token punctuation">.</span>wolfcode<span class="token punctuation">.</span>domain</span><span class="token punctuation">;</span>

<span class="token keyword">import</span> <span class="token import"><span class="token namespace">com<span class="token punctuation">.</span>baomidou<span class="token punctuation">.</span>mybatisplus<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span></span><span class="token class-name">IdType</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">com<span class="token punctuation">.</span>baomidou<span class="token punctuation">.</span>mybatisplus<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span></span><span class="token class-name">TableId</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">lombok<span class="token punctuation">.</span></span><span class="token class-name">Getter</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">lombok<span class="token punctuation">.</span></span><span class="token class-name">Setter</span></span><span class="token punctuation">;</span>

<span class="token keyword">import</span> <span class="token import"><span class="token namespace">java<span class="token punctuation">.</span>io<span class="token punctuation">.</span></span><span class="token class-name">Serializable</span></span><span class="token punctuation">;</span>

<span class="token comment">//订单</span>
<span class="token annotation punctuation">@Getter</span>
<span class="token annotation punctuation">@Setter</span>
<span class="token annotation punctuation">@ToString</span>
<span class="token annotation punctuation">@TableName</span><span class="token punctuation">(</span><span class="token string">&quot;t_order&quot;</span><span class="token punctuation">)</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Order</span> <span class="token keyword">implements</span> <span class="token class-name">Serializable</span> <span class="token punctuation">{</span>

    <span class="token annotation punctuation">@TableId</span><span class="token punctuation">(</span>type <span class="token operator">=</span> <span class="token class-name">IdType</span><span class="token punctuation">.</span><span class="token constant">AUTO</span><span class="token punctuation">)</span>
    <span class="token keyword">private</span> <span class="token class-name">Long</span> id<span class="token punctuation">;</span><span class="token comment">//订单id</span>
    <span class="token comment">//用户</span>
    <span class="token keyword">private</span> <span class="token class-name">Long</span> uid<span class="token punctuation">;</span><span class="token comment">//用户id</span>
    <span class="token keyword">private</span> <span class="token class-name">String</span> username<span class="token punctuation">;</span><span class="token comment">//用户名</span>
    <span class="token comment">//商品</span>
    <span class="token keyword">private</span> <span class="token class-name">Long</span> pid<span class="token punctuation">;</span><span class="token comment">//商品id</span>
    <span class="token keyword">private</span> <span class="token class-name">String</span> productName<span class="token punctuation">;</span><span class="token comment">//商品名称</span>
    <span class="token keyword">private</span> <span class="token class-name">Double</span> productPrice<span class="token punctuation">;</span><span class="token comment">//商品单价</span>
    <span class="token comment">//数量</span>
    <span class="token keyword">private</span> <span class="token class-name">Integer</span> number<span class="token punctuation">;</span><span class="token comment">//购买数量</span>
<span class="token punctuation">}</span>

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br></div></div><p><strong>3.创建shop-order-server项目，然后在pom.xml文件中添加下面内容</strong></p> <div class="language-xml line-numbers-mode"><pre class="language-xml"><code><span class="token prolog">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>project</span> <span class="token attr-name">xmlns</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>http://maven.apache.org/POM/4.0.0<span class="token punctuation">&quot;</span></span>
         <span class="token attr-name"><span class="token namespace">xmlns:</span>xsi</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>http://www.w3.org/2001/XMLSchema-instance<span class="token punctuation">&quot;</span></span>
         <span class="token attr-name"><span class="token namespace">xsi:</span>schemaLocation</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>parent</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>shop-parent<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>cn.wolfcode<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">&gt;</span></span>1.0.0<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>parent</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>modelVersion</span><span class="token punctuation">&gt;</span></span>4.0.0<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>modelVersion</span><span class="token punctuation">&gt;</span></span>

    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>shop-order-server<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependencies</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">&gt;</span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>org.springframework.boot<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>spring-boot-starter-web<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">&gt;</span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>mysql<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>mysql-connector-java<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">&gt;</span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>com.alibaba<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>fastjson<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">&gt;</span></span>1.2.56<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">&gt;</span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>cn.wolfcode<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>shop-order-api<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">&gt;</span></span>1.0.0<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependencies</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>project</span><span class="token punctuation">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br></div></div><p><strong>4.编写配置文件application.yml</strong></p> <div class="language-yaml line-numbers-mode"><pre class="language-yaml"><code><span class="token key atrule">server</span><span class="token punctuation">:</span>
  <span class="token key atrule">port</span><span class="token punctuation">:</span> <span class="token number">8091</span>
<span class="token key atrule">spring</span><span class="token punctuation">:</span>
  <span class="token key atrule">application</span><span class="token punctuation">:</span>
    <span class="token key atrule">name</span><span class="token punctuation">:</span> order<span class="token punctuation">-</span>service
  <span class="token key atrule">datasource</span><span class="token punctuation">:</span>
    <span class="token key atrule">driver-class-name</span><span class="token punctuation">:</span> com.mysql.cj.jdbc.Driver
    <span class="token key atrule">url</span><span class="token punctuation">:</span> jdbc<span class="token punctuation">:</span>mysql<span class="token punctuation">:</span>///shop<span class="token punctuation">-</span>order<span class="token punctuation">?</span>serverTimezone=UTC<span class="token important">&amp;useUnicode=true&amp;characterEncoding=utf-8&amp;useSSL=true</span>
    <span class="token key atrule">username</span><span class="token punctuation">:</span> root
    <span class="token key atrule">password</span><span class="token punctuation">:</span> admin
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><p><strong>5.在数据库中创建shop-order的数据库</strong></p> <div class="language-sql line-numbers-mode"><pre class="language-sql"><code><span class="token keyword">CREATE</span> <span class="token keyword">TABLE</span> <span class="token identifier"><span class="token punctuation">`</span>t_order<span class="token punctuation">`</span></span> <span class="token punctuation">(</span>
  <span class="token identifier"><span class="token punctuation">`</span>id<span class="token punctuation">`</span></span> <span class="token keyword">bigint</span> <span class="token operator">NOT</span> <span class="token boolean">NULL</span> <span class="token keyword">AUTO_INCREMENT</span> <span class="token keyword">COMMENT</span> <span class="token string">'主键'</span><span class="token punctuation">,</span>
  <span class="token identifier"><span class="token punctuation">`</span>uid<span class="token punctuation">`</span></span> <span class="token keyword">bigint</span> <span class="token keyword">DEFAULT</span> <span class="token boolean">NULL</span> <span class="token keyword">COMMENT</span> <span class="token string">'用户id'</span><span class="token punctuation">,</span>
  <span class="token identifier"><span class="token punctuation">`</span>username<span class="token punctuation">`</span></span> <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">255</span><span class="token punctuation">)</span> <span class="token keyword">DEFAULT</span> <span class="token boolean">NULL</span> <span class="token keyword">COMMENT</span> <span class="token string">'用户名称'</span><span class="token punctuation">,</span>
  <span class="token identifier"><span class="token punctuation">`</span>pid<span class="token punctuation">`</span></span> <span class="token keyword">bigint</span> <span class="token keyword">DEFAULT</span> <span class="token boolean">NULL</span> <span class="token keyword">COMMENT</span> <span class="token string">'商品id'</span><span class="token punctuation">,</span>
  <span class="token identifier"><span class="token punctuation">`</span>product_name<span class="token punctuation">`</span></span> <span class="token keyword">varchar</span><span class="token punctuation">(</span><span class="token number">255</span><span class="token punctuation">)</span> <span class="token keyword">DEFAULT</span> <span class="token boolean">NULL</span> <span class="token keyword">COMMENT</span> <span class="token string">'商品名称'</span><span class="token punctuation">,</span>
  <span class="token identifier"><span class="token punctuation">`</span>product_price<span class="token punctuation">`</span></span> <span class="token keyword">double</span><span class="token punctuation">(</span><span class="token number">255</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">)</span> <span class="token keyword">DEFAULT</span> <span class="token boolean">NULL</span> <span class="token keyword">COMMENT</span> <span class="token string">'商品单价'</span><span class="token punctuation">,</span>
  <span class="token identifier"><span class="token punctuation">`</span>number<span class="token punctuation">`</span></span> <span class="token keyword">int</span> <span class="token keyword">DEFAULT</span> <span class="token boolean">NULL</span> <span class="token keyword">COMMENT</span> <span class="token string">'购买数量'</span><span class="token punctuation">,</span>
  <span class="token keyword">PRIMARY</span> <span class="token keyword">KEY</span> <span class="token punctuation">(</span><span class="token identifier"><span class="token punctuation">`</span>id<span class="token punctuation">`</span></span><span class="token punctuation">)</span>
<span class="token punctuation">)</span> <span class="token keyword">ENGINE</span><span class="token operator">=</span><span class="token keyword">InnoDB</span> <span class="token keyword">DEFAULT</span> <span class="token keyword">CHARSET</span><span class="token operator">=</span>utf8mb3<span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><p><strong>6.创建OrderMapper</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">package</span> <span class="token namespace">cn<span class="token punctuation">.</span>wolfcode<span class="token punctuation">.</span>mapper</span><span class="token punctuation">;</span>

<span class="token keyword">import</span> <span class="token import"><span class="token namespace">cn<span class="token punctuation">.</span>wolfcode<span class="token punctuation">.</span>domain<span class="token punctuation">.</span></span><span class="token class-name">Order</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">com<span class="token punctuation">.</span>baomidou<span class="token punctuation">.</span>mybatisplus<span class="token punctuation">.</span>core<span class="token punctuation">.</span>mapper<span class="token punctuation">.</span></span><span class="token class-name">BaseMapper</span></span><span class="token punctuation">;</span>

<span class="token keyword">public</span> <span class="token keyword">interface</span> <span class="token class-name">OrderMapper</span> <span class="token keyword">extends</span> <span class="token class-name">BaseMapper</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Order</span><span class="token punctuation">&gt;</span></span> <span class="token punctuation">{</span>
<span class="token punctuation">}</span>

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p><strong>7.创建OrderService接口和实现类</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">package</span> <span class="token namespace">cn<span class="token punctuation">.</span>wolfcode<span class="token punctuation">.</span>service</span><span class="token punctuation">;</span>

<span class="token keyword">import</span> <span class="token import"><span class="token namespace">cn<span class="token punctuation">.</span>wolfcode<span class="token punctuation">.</span>domain<span class="token punctuation">.</span></span><span class="token class-name">Order</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">com<span class="token punctuation">.</span>baomidou<span class="token punctuation">.</span>mybatisplus<span class="token punctuation">.</span>extension<span class="token punctuation">.</span>service<span class="token punctuation">.</span></span><span class="token class-name">IService</span></span><span class="token punctuation">;</span>

<span class="token keyword">public</span> <span class="token keyword">interface</span> <span class="token class-name">IOrderService</span>  <span class="token keyword">extends</span> <span class="token class-name">IService</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Order</span><span class="token punctuation">&gt;</span></span> <span class="token punctuation">{</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">package</span> <span class="token namespace">cn<span class="token punctuation">.</span>wolfcode<span class="token punctuation">.</span>service<span class="token punctuation">.</span>impl</span><span class="token punctuation">;</span>

<span class="token keyword">import</span> <span class="token import"><span class="token namespace">cn<span class="token punctuation">.</span>wolfcode<span class="token punctuation">.</span>domain<span class="token punctuation">.</span></span><span class="token class-name">Order</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">cn<span class="token punctuation">.</span>wolfcode<span class="token punctuation">.</span>mapper<span class="token punctuation">.</span></span><span class="token class-name">OrderMapper</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">cn<span class="token punctuation">.</span>wolfcode<span class="token punctuation">.</span>service<span class="token punctuation">.</span></span><span class="token class-name">IOrderService</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">com<span class="token punctuation">.</span>baomidou<span class="token punctuation">.</span>mybatisplus<span class="token punctuation">.</span>extension<span class="token punctuation">.</span>service<span class="token punctuation">.</span>impl<span class="token punctuation">.</span></span><span class="token class-name">ServiceImpl</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>stereotype<span class="token punctuation">.</span></span><span class="token class-name">Service</span></span><span class="token punctuation">;</span>

<span class="token annotation punctuation">@Service</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">OrderServiceImpl</span> <span class="token keyword">extends</span> <span class="token class-name">ServiceImpl</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">OrderMapper</span><span class="token punctuation">,</span> <span class="token class-name">Order</span><span class="token punctuation">&gt;</span></span> <span class="token keyword">implements</span> <span class="token class-name">IOrderService</span> <span class="token punctuation">{</span>
<span class="token punctuation">}</span>

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><p><strong>8.创建Controller</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@RestController</span>
<span class="token annotation punctuation">@RequestMapping</span><span class="token punctuation">(</span><span class="token string">&quot;orders&quot;</span><span class="token punctuation">)</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">OrderController</span> <span class="token punctuation">{</span>
    <span class="token annotation punctuation">@Autowired</span>
    <span class="token keyword">private</span> <span class="token class-name">IOrderService</span> orderService<span class="token punctuation">;</span>
    <span class="token annotation punctuation">@GetMapping</span><span class="token punctuation">(</span><span class="token string">&quot;/save&quot;</span><span class="token punctuation">)</span>  <span class="token comment">//测试方便使用Get方式</span>
    <span class="token keyword">public</span> <span class="token class-name">Order</span> <span class="token function">order</span><span class="token punctuation">(</span><span class="token class-name">Long</span> pid<span class="token punctuation">,</span> <span class="token class-name">Long</span> uid<span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token keyword">return</span> orderService<span class="token punctuation">.</span><span class="token function">createOrder</span><span class="token punctuation">(</span>pid<span class="token punctuation">,</span> uid<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><p><strong>9.编写启动类OrderServer.java</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">package</span> <span class="token namespace">cn<span class="token punctuation">.</span>wolfcode</span><span class="token punctuation">;</span>

<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>mybatis<span class="token punctuation">.</span>spring<span class="token punctuation">.</span>annotation<span class="token punctuation">.</span></span><span class="token class-name">MapperScan</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>boot<span class="token punctuation">.</span></span><span class="token class-name">SpringApplication</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>boot<span class="token punctuation">.</span>autoconfigure<span class="token punctuation">.</span></span><span class="token class-name">SpringBootApplication</span></span><span class="token punctuation">;</span>

<span class="token annotation punctuation">@SpringBootApplication</span>
<span class="token annotation punctuation">@MapperScan</span><span class="token punctuation">(</span><span class="token string">&quot;cn.wolfcode.mapper&quot;</span><span class="token punctuation">)</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">OrderServer</span> <span class="token punctuation">{</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">SpringApplication</span><span class="token punctuation">.</span><span class="token function">run</span><span class="token punctuation">(</span><span class="token class-name">OrderServer</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">,</span>args<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br></div></div><h3 id="_3-8-服务间如何进行远程调用"><a href="#_3-8-服务间如何进行远程调用" class="header-anchor">#</a> 3.8 服务间如何进行远程调用</h3> <p>现在存在一个问题，Order-server服务创建订单操作需要配置商品信息，此时怎么办？</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@Service</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">OrderServiceImpl</span> <span class="token keyword">extends</span> <span class="token class-name">ServiceImpl</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">OrderMapper</span><span class="token punctuation">,</span> <span class="token class-name">Order</span><span class="token punctuation">&gt;</span></span> <span class="token keyword">implements</span> <span class="token class-name">IOrderService</span> <span class="token punctuation">{</span>
    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">public</span> <span class="token class-name">Order</span> <span class="token function">createOrder</span><span class="token punctuation">(</span><span class="token class-name">Long</span> pid<span class="token punctuation">,</span> <span class="token class-name">Long</span> uid<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">Order</span> order <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Order</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//商品？？</span>
        <span class="token class-name">Product</span> product <span class="token operator">=</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
        order<span class="token punctuation">.</span><span class="token function">setPid</span><span class="token punctuation">(</span>pid<span class="token punctuation">)</span><span class="token punctuation">;</span>
        order<span class="token punctuation">.</span><span class="token function">setProductName</span><span class="token punctuation">(</span>product<span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        order<span class="token punctuation">.</span><span class="token function">setProductPrice</span><span class="token punctuation">(</span>product<span class="token punctuation">.</span><span class="token function">getPrice</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">//用户</span>
        order<span class="token punctuation">.</span><span class="token function">setUid</span><span class="token punctuation">(</span><span class="token number">1L</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        order<span class="token punctuation">.</span><span class="token function">setUsername</span><span class="token punctuation">(</span><span class="token string">&quot;dafei&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        order<span class="token punctuation">.</span><span class="token function">setNumber</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">super</span><span class="token punctuation">.</span><span class="token function">save</span><span class="token punctuation">(</span>order<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> order<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br></div></div><p>思考，谁能提供商品信息查询逻辑呢？答案：product-server， 问题来了，怎么调用？这里引入一个新问题：服务与服务间如何调用(交互)？</p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20221013100523503.png" alt="image-20221013100523503"></strong></p> <p>问题来了，怎么用java代码调用发起http接口调用嗯？？答案是：<strong>RestTemplate</strong></p> <p><strong>RestTempate 是SpringMVC提供专门用于访问http请求的工具类</strong></p> <p>1.在shop-order-server项目启动类上添加RestTemplate的bean配置</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@SpringBootApplication</span>
<span class="token annotation punctuation">@MapperScan</span><span class="token punctuation">(</span><span class="token string">&quot;cn.wolfcode.mapper&quot;</span><span class="token punctuation">)</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">OrderServer</span> <span class="token punctuation">{</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">SpringApplication</span><span class="token punctuation">.</span><span class="token function">run</span><span class="token punctuation">(</span><span class="token class-name">OrderServer</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">,</span>args<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token annotation punctuation">@Bean</span>
    <span class="token keyword">public</span> <span class="token class-name">RestTemplate</span> <span class="token function">restTemplate</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">RestTemplate</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><p>2.在OrderServiceImpl中注入RestTemplate并实现远程调用</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@Service</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">OrderServiceImpl</span> <span class="token keyword">extends</span> <span class="token class-name">ServiceImpl</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">OrderMapper</span><span class="token punctuation">,</span> <span class="token class-name">Order</span><span class="token punctuation">&gt;</span></span> <span class="token keyword">implements</span> <span class="token class-name">IOrderService</span> <span class="token punctuation">{</span>

    <span class="token annotation punctuation">@Autowired</span>
    <span class="token keyword">private</span> <span class="token class-name">RestTemplate</span> restTemplate<span class="token punctuation">;</span>

    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">public</span> <span class="token class-name">Order</span> <span class="token function">createOrder</span><span class="token punctuation">(</span><span class="token class-name">Long</span> pid<span class="token punctuation">,</span> <span class="token class-name">Long</span> uid<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">Order</span> order <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Order</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//商品</span>
        <span class="token comment">//方案1：通过restTemplate方式</span>
        <span class="token class-name">String</span> url  <span class="token operator">=</span> <span class="token string">&quot;http://localhost:8081/products/&quot;</span> <span class="token operator">+</span> pid<span class="token punctuation">;</span>
        <span class="token class-name">Product</span> product <span class="token operator">=</span> restTemplate<span class="token punctuation">.</span><span class="token function">getForObject</span><span class="token punctuation">(</span>url<span class="token punctuation">,</span> <span class="token class-name">Product</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        order<span class="token punctuation">.</span><span class="token function">setPid</span><span class="token punctuation">(</span>pid<span class="token punctuation">)</span><span class="token punctuation">;</span>
        order<span class="token punctuation">.</span><span class="token function">setProductName</span><span class="token punctuation">(</span>product<span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        order<span class="token punctuation">.</span><span class="token function">setProductPrice</span><span class="token punctuation">(</span>product<span class="token punctuation">.</span><span class="token function">getPrice</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">//用户</span>
        order<span class="token punctuation">.</span><span class="token function">setUid</span><span class="token punctuation">(</span><span class="token number">1L</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        order<span class="token punctuation">.</span><span class="token function">setUsername</span><span class="token punctuation">(</span><span class="token string">&quot;dafei&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        order<span class="token punctuation">.</span><span class="token function">setNumber</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>order<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">super</span><span class="token punctuation">.</span><span class="token function">save</span><span class="token punctuation">(</span>order<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> order<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br></div></div><p>上面操作确实完成的服务间调用问题，但是代码很不优雅，存在着一定小瑕疵，比如：ip，端口变了呢？</p> <ul><li><p>一旦服务提供者(商品服务)地址变化，就需要手工修改代码</p></li> <li><p>一旦是多个服务(商品服务)提供者，无法实现负载均衡功能</p></li> <li><p>一旦服务变得越来越多，人工维护调用关系困难</p></li></ul> <p>那怎么办呢， 这时候得引入SpringCloud Alibaba第一个组件：</p> <p>**组件：**注册中心--<strong>Nacos</strong></p> <p><strong>功能</strong>：动态的实现<strong>服务治理</strong>。</p> <h2 id="四、服务治理-nacos-discovery"><a href="#四、服务治理-nacos-discovery" class="header-anchor">#</a> 四、服务治理  Nacos Discovery</h2> <h3 id="_4-1-什么是服务治理"><a href="#_4-1-什么是服务治理" class="header-anchor">#</a> 4.1 什么是服务治理</h3> <p>服务治理是微服务架构中最核心最基本的模块。用于实现各个微服务的<strong>自动化注册与发现</strong>。</p> <p><strong>大白话：各种微服务ip/端口的统一管理(CRUD)</strong></p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20221013105227696.png" alt="image-20221013105227696"></p> <p><strong>服务注册：<strong>在服务治理框架中，都会构建一个</strong>注册中心</strong>(管理ip/端口的地方)，每个服务单元向注册中心登记自己提供服</p> <p>务的详细信息。并在注册中心形成一张服务的清单(<strong>建立服务名与ip/端口映射</strong>)，服务注册中心需要以心跳的方式去监测清单中</p> <p>的服务是否可用，如果不可用，需要在服务清单中剔除不可用的服务。</p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20221013110453454.png" alt="image-20221013110453454"></p> <p>**服务发现：**微服务去咨询注册中心，并获取所有服务清单，实现对具体服务的访问。</p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20201029084800199.png" alt="image-20201029084800199"></strong></p> <p>总结一下：在微服务架构里服务中心主要起到了<strong>协调者</strong>的作用。功能有：</p> <ol><li><p>服务发现：</p> <p><strong>服务注册</strong>：保存服务提供者和服务调用者的信息</p> <p><strong>服务订阅</strong>：服务调用者订阅服务提供者的信息，注册中心向订阅者推送提供者的信息（<strong>拉服务清单</strong>）</p></li> <li><p>服务健康检测</p> <p>检测服务提供者的健康情况，如果发现异常，执行服务剔除</p></li></ol> <h3 id="_4-2-常见注册中心"><a href="#_4-2-常见注册中心" class="header-anchor">#</a> 4.2 常见注册中心</h3> <ul><li><p><strong>Zookeeper</strong></p> <p>Zookeeper是一个分布式服务框架，是Apache Hadoop 的一个子项目，它主要是用来解决分布式</p></li></ul> <p>应用中经常遇到的一些数据管理问题，如：统一命名服务、状态同步服务、集群管理、分布式应用</p> <p>配置项的管理等。</p> <ul><li><p><strong>Eureka</strong></p> <p>Eureka是Springcloud Netflflix中的重要组件，主要作用就是做服务注册和发现。但是现在已经闭</p></li></ul> <p>源</p> <ul><li><p><strong>Consul</strong></p> <p>Consul是基于GO语言开发的开源工具，主要面向分布式，服务化的系统提供服务注册、服务发现</p></li></ul> <p>和配置管理的功能。Consul的功能都很实用，其中包括：服务注册/发现、健康检查、Key/Value</p> <p>存储、多数据中心和分布式一致性保证等特性。Consul本身只是一个二进制的可执行文件，所以</p> <p>安装和部署都非常简单，只需要从官网下载后，在执行对应的启动脚本即可。</p> <ul><li><p><strong>Nacos</strong></p> <p>Nacos是一个更易于构建云原生应用的动态服务发现、配置管理和服务管理平台。它是 Spring</p></li></ul> <p>Cloud Alibaba 组件之一，负责服务注册发现和服务配置。</p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/20190916151417612.png" alt="img"></p> <h3 id="_4-3-nacos-简介"><a href="#_4-3-nacos-简介" class="header-anchor">#</a> 4.3 Nacos 简介</h3> <p>Nacos 致力于帮助您发现、配置和管理微服务。Nacos 提供了一组简单易用的特性集，帮助您快速</p> <p>实现动态服务发现、服务配置、服务元数据及流量管理。</p> <p>从上面的介绍就可以看出，Nacos的作用就是一个注册中心，用来管理注册上来的各个微服务。</p> <p><strong>核心功能点</strong>:</p> <ul><li><p><strong>服务注册</strong>:  Nacos Client会通过发送REST请求想Nacos Server注册自己的服务，提供自身的元数据，比如IP地址，端口等信息。Nacos Server接收到注册请求后，就会把这些元数据存储到一个双层的内存Map中。</p></li> <li><p><strong>服务心跳</strong>:  在服务注册后，Nacos Client会维护一个定时心跳来维持统治Nacos Server,说明服务一致处于可用状态，防止被剔除，默认5s发送一次心跳</p></li> <li><p><strong>服务同步</strong>:  Nacos Server集群之间会相互同步服务实例，用来保证服务信息的一致性。</p></li> <li><p><strong>服务发现</strong>： 服务消费者(Nacos Client)在调用服务提供的服务时，会发送一个REST请求给Nacos Server,获取上面注册的服务清单，并且缓存在Nacos Client本地,同时会在Nacos Client本地开启一个定时任务拉取服务最新的注册表信息更新到本地缓存。</p></li> <li><p><strong>服务健康检查</strong>:  Nacos Server 会开启一个定时任务来检查注册服务实例的健康情况，对于超过15s没有收到客户端心跳的实例会将他的healthy属性设置为false(客户端服务发现时不会发现)，如果某个实例超过30s没有收到心跳，直接剔除该实例(被剔除的实例如果恢复发送心跳则会重新注册)</p></li></ul> <h3 id="_4-4-nacos实战入门"><a href="#_4-4-nacos实战入门" class="header-anchor">#</a> 4.4 Nacos实战入门</h3> <p>接下来，我们就在现有的环境中加入nacos，并将我们的两个微服务注册上去。</p> <h4 id="_4-4-1-搭建nacos环境"><a href="#_4-4-1-搭建nacos环境" class="header-anchor">#</a> 4.4.1 搭建Nacos环境</h4> <ol><li><p>安装Nacos</p> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code>下载地址: https://github.com/alibaba/nacos/releases 
下载zip格式的安装包，然后进行解压缩操作,上课使用的Nacos Server版本是1.3.2
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div></li> <li><p>启动Nacos</p> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code><span class="token comment">#切换目录 </span>
<span class="token builtin class-name">cd</span> nacos/bin 
<span class="token comment">#命令启动 </span>
startup.cmd <span class="token parameter variable">-m</span> standalone
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div></li> <li><p>访问Nacos</p> <p>打开浏览器输入http://localhost:8848/nacos，即可访问服务， 默认密码是nacos/nacos</p></li></ol> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20201029092937927.png" alt="image-20201029092937927"></strong></p> <h4 id="_4-4-2-将商品服务注册到nacos"><a href="#_4-4-2-将商品服务注册到nacos" class="header-anchor">#</a> 4.4.2 将商品服务注册到Nacos</h4> <p>接下来开始修改 shop-product-server 模块的代码， 将其注册到nacos服务上</p> <ol><li><strong>在pom.xml中添加Nacos的依赖</strong></li></ol> <div class="language-xml line-numbers-mode"><pre class="language-xml"><code><span class="token comment">&lt;!--nacos客户端--&gt;</span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">&gt;</span></span>
	<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>com.alibaba.cloud<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
	<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>spring-cloud-starter-alibaba-nacos-discovery<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><ol start="2"><li><p><strong>在主类上添加@EnableDiscoveryClient注解</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@SpringBootApplication</span>
<span class="token annotation punctuation">@MapperScan</span><span class="token punctuation">(</span><span class="token string">&quot;cn.wolfcode.mapper&quot;</span><span class="token punctuation">)</span>
<span class="token annotation punctuation">@EnableDiscoveryClient</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">ProductServer</span> <span class="token punctuation">{</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">SpringApplication</span><span class="token punctuation">.</span><span class="token function">run</span><span class="token punctuation">(</span><span class="token class-name">ProductServer</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">,</span>args<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div></li> <li><p><strong>在application.yml中添加Nacos服务的地址</strong></p> <div class="language-yaml line-numbers-mode"><pre class="language-yaml"><code><span class="token key atrule">spring</span><span class="token punctuation">:</span>
  <span class="token key atrule">cloud</span><span class="token punctuation">:</span> 
    <span class="token key atrule">nacos</span><span class="token punctuation">:</span> 
      <span class="token key atrule">discovery</span><span class="token punctuation">:</span> 
        <span class="token key atrule">server-addr</span><span class="token punctuation">:</span> localhost<span class="token punctuation">:</span><span class="token number">8848</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div></li> <li><p><strong>启动服务， 观察Nacos的控制面板中是否有注册上来的商品微服务</strong></p></li></ol> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20201029094004073.png" alt="image-20201029094004073"></strong></p> <h4 id="_4-4-3-将订单服务注册到nacos"><a href="#_4-4-3-将订单服务注册到nacos" class="header-anchor">#</a> 4.4.3 将订单服务注册到Nacos</h4> <p>接下来开始修改 shop-order-server 模块的代码， 将其注册到nacos服务上</p> <ol><li><strong>在pom.xml中添加Nacos的依赖</strong></li></ol> <div class="language-xml line-numbers-mode"><pre class="language-xml"><code><span class="token comment">&lt;!--nacos客户端--&gt;</span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">&gt;</span></span>
	<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>com.alibaba.cloud<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
	<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>spring-cloud-starter-alibaba-nacos-discovery<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><ol start="2"><li><p><strong>在主类上添加@EnableDiscoveryClient注解</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@SpringBootApplication</span>
<span class="token annotation punctuation">@EnableDiscoveryClient</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">OrderServer</span> <span class="token punctuation">{</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">SpringApplication</span><span class="token punctuation">.</span><span class="token function">run</span><span class="token punctuation">(</span><span class="token class-name">OrderServer</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">,</span>args<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div></li> <li><p><strong>在application.yml中添加Nacos服务的地址</strong></p></li></ol> <div class="language-yaml line-numbers-mode"><pre class="language-yaml"><code><span class="token key atrule">spring</span><span class="token punctuation">:</span>
  <span class="token key atrule">cloud</span><span class="token punctuation">:</span> 
    <span class="token key atrule">nacos</span><span class="token punctuation">:</span> 
      <span class="token key atrule">discovery</span><span class="token punctuation">:</span> 
        <span class="token key atrule">server-addr</span><span class="token punctuation">:</span> localhost<span class="token punctuation">:</span><span class="token number">8848</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><ol start="4"><li><strong>启动服务， 观察Nacos的控制面板中是否有注册上来的订单微服务</strong></li></ol> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20201029094943183.png" alt="image-20201029094943183"></strong></p> <ol start="5"><li><strong>修改OrderServiceImpl， 实现微服务调用</strong></li></ol> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@Service</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">OrderServiceImpl</span> <span class="token keyword">extends</span> <span class="token class-name">ServiceImpl</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">OrderMapper</span><span class="token punctuation">,</span> <span class="token class-name">Order</span><span class="token punctuation">&gt;</span></span> <span class="token keyword">implements</span> <span class="token class-name">IOrderService</span> <span class="token punctuation">{</span>

    <span class="token annotation punctuation">@Autowired</span>
    <span class="token keyword">private</span> <span class="token class-name">RestTemplate</span> restTemplate<span class="token punctuation">;</span>

    <span class="token annotation punctuation">@Autowired</span>
    <span class="token keyword">private</span> <span class="token class-name">DiscoveryClient</span> discoveryClient<span class="token punctuation">;</span>

    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">public</span> <span class="token class-name">Order</span> <span class="token function">createOrder</span><span class="token punctuation">(</span><span class="token class-name">Long</span> pid<span class="token punctuation">,</span> <span class="token class-name">Long</span> uid<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">Order</span> order <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Order</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//商品</span>
        <span class="token comment">//方案1：通过restTemplate方式</span>
        <span class="token comment">//String url  = &quot;http://localhost:8081/products/&quot; + pid;</span>
        <span class="token comment">//Product product = restTemplate.getForObject(url, Product.class);</span>

        <span class="token comment">//方案2：使用注册中心api方式-discoveryClient</span>
        <span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">ServiceInstance</span><span class="token punctuation">&gt;</span></span> instances <span class="token operator">=</span> discoveryClient<span class="token punctuation">.</span><span class="token function">getInstances</span><span class="token punctuation">(</span><span class="token string">&quot;product-service&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">ServiceInstance</span> instance <span class="token operator">=</span> instances<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  <span class="token comment">//当前只有一个</span>
        <span class="token class-name">String</span> host <span class="token operator">=</span> instance<span class="token punctuation">.</span><span class="token function">getHost</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">int</span> port <span class="token operator">=</span> instance<span class="token punctuation">.</span><span class="token function">getPort</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">String</span> url <span class="token operator">=</span> <span class="token string">&quot;http://&quot;</span> <span class="token operator">+</span> host <span class="token operator">+</span> <span class="token string">&quot;:&quot;</span> <span class="token operator">+</span> port <span class="token operator">+</span> <span class="token string">&quot;/products/&quot;</span> <span class="token operator">+</span> pid<span class="token punctuation">;</span>
        <span class="token class-name">Product</span> product <span class="token operator">=</span> restTemplate<span class="token punctuation">.</span><span class="token function">getForObject</span><span class="token punctuation">(</span>url<span class="token punctuation">,</span> <span class="token class-name">Product</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        order<span class="token punctuation">.</span><span class="token function">setPid</span><span class="token punctuation">(</span>pid<span class="token punctuation">)</span><span class="token punctuation">;</span>
        order<span class="token punctuation">.</span><span class="token function">setProductName</span><span class="token punctuation">(</span>product<span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        order<span class="token punctuation">.</span><span class="token function">setProductPrice</span><span class="token punctuation">(</span>product<span class="token punctuation">.</span><span class="token function">getPrice</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">//用户</span>
        order<span class="token punctuation">.</span><span class="token function">setUid</span><span class="token punctuation">(</span><span class="token number">1L</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        order<span class="token punctuation">.</span><span class="token function">setUsername</span><span class="token punctuation">(</span><span class="token string">&quot;dafei&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        order<span class="token punctuation">.</span><span class="token function">setNumber</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>order<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">super</span><span class="token punctuation">.</span><span class="token function">save</span><span class="token punctuation">(</span>order<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> order<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br></div></div><h2 id="五、远程调用负载均衡-ribbon"><a href="#五、远程调用负载均衡-ribbon" class="header-anchor">#</a> 五、远程调用负载均衡 Ribbon</h2> <h3 id="_5-1-背景铺垫"><a href="#_5-1-背景铺垫" class="header-anchor">#</a> 5.1 背景铺垫</h3> <p>微服务项目有一个重要的功能：可以很容易<strong>动态扩缩容</strong></p> <p>这里我们不谈容器技术(docker/k8s这些)，传统的扩缩容简单理解就是服务集群。</p> <p>**扩：**特定时期(比如促销，天灾人祸)一个微服务可能容易挂掉(撑不住/宕机)，那么多开几个就行，此为扩。</p> <p>**缩：**特定时期过后，多开的微服务可以适当关掉多余的，此为缩</p> <p>这里考虑扩的问题，以订单与商品服务为例子，</p> <p>设想一种场景：如果某天公司要促销，单一的订单服务，商品服务不一定能撑得住，该怎么扩容。</p> <p>扩容常用的手段就是集群，如下：</p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20221013155012725.png" alt="image-20221013155012725"></p> <p>集群存在一个很大问题，客户端发起的请求让哪个微服务处理？解决方案：<strong>负载均衡服务器</strong></p> <h3 id="_5-2-什么是负载均衡"><a href="#_5-2-什么是负载均衡" class="header-anchor">#</a> 5.2 什么是负载均衡</h3> <p>通俗的讲， 负载均衡就是将负载（工作任务，访问请求）进行分摊到多个操作单元（服务器,组件）上进行执行。</p> <p>根据负载均衡发生位置的不同,一般分为<strong>服务端负载均衡</strong>和<strong>客户端负载均衡</strong>。</p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20201029102235075.png" alt="image-20201029102235075"></strong></p> <p>服务端负载均衡指的是发生在服务提供者一方,比如常见的Nginx负载均衡</p> <p><strong>服务端负载均衡</strong></p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20221013161421850.png" alt="image-20221013161421850"></p> <p>而客户端负载均衡指的是发生在服务请求的一方，也就是在发送请求之前已经选好了由哪个实例处理请求</p> <p><strong>客户端负载均衡</strong></p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20221013164501474.png" alt="image-20221013164501474"></p> <p>微服务调用关系中一般会选择客<strong>户端负载均衡</strong>，也就是在服务调用的一方来决定服务由哪个提供者执行。</p> <p><strong>那问题来：代码上如何实现？</strong></p> <h3 id="_5-3-自定义负载均衡"><a href="#_5-3-自定义负载均衡" class="header-anchor">#</a> 5.3 自定义负载均衡</h3> <p>需求：开启2个商品客户端(localhost:8081/localhost:8082)，这里说明一下，真实开发服务器可定是部署在不同的服务器中，ip不一样，端口可以一样。此时为学习，只有一台电脑，使用不同端口模拟一下。</p> <ol><li><p><strong>通过idea再启动一个 shop-product 微服务，设置其端口为8082： -Dserver.port=8082</strong></p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20221013171010744.png" alt="image-20221013171010744"></strong></p></li> <li><p><strong>通过nacos查看微服务的启动情况</strong></p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20201029102905790.png" alt="image-20201029102905790"></strong></p></li> <li><p><strong>修改 OrderServiceImpl 的代码，实现负载均衡</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@Service</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">OrderServiceImpl</span> <span class="token keyword">extends</span> <span class="token class-name">ServiceImpl</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">OrderMapper</span><span class="token punctuation">,</span> <span class="token class-name">Order</span><span class="token punctuation">&gt;</span></span> <span class="token keyword">implements</span> <span class="token class-name">IOrderService</span> <span class="token punctuation">{</span>

    <span class="token annotation punctuation">@Autowired</span>
    <span class="token keyword">private</span> <span class="token class-name">RestTemplate</span> restTemplate<span class="token punctuation">;</span>

    <span class="token annotation punctuation">@Autowired</span>
    <span class="token keyword">private</span> <span class="token class-name">DiscoveryClient</span> discoveryClient<span class="token punctuation">;</span>

    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">public</span> <span class="token class-name">Order</span> <span class="token function">createOrder</span><span class="token punctuation">(</span><span class="token class-name">Long</span> pid<span class="token punctuation">,</span> <span class="token class-name">Long</span> uid<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">Order</span> order <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Order</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//商品</span>
        <span class="token comment">//方案1：通过restTemplate方式</span>
        <span class="token comment">//String url  = &quot;http://localhost:8081/products/&quot; + pid;</span>
        <span class="token comment">//Product product = restTemplate.getForObject(url, Product.class);</span>


        <span class="token comment">//方案2：使用注册中心api方式-discoveryClient</span>
        <span class="token comment">//List&lt;ServiceInstance&gt; instances = discoveryClient.getInstances(&quot;product-service&quot;);</span>
        <span class="token comment">//ServiceInstance instance = instances.get(0);  //当前只有一个</span>
        <span class="token comment">//String host = instance.getHost();</span>
        <span class="token comment">//int port = instance.getPort();</span>
        <span class="token comment">//String url = &quot;http://&quot; + host + &quot;:&quot; + port + &quot;/products/&quot; + pid;</span>
        <span class="token comment">//Product product = restTemplate.getForObject(url, Product.class);</span>

        <span class="token comment">//方案3：使用注册中心api方式-discoveryClient--带负载均衡</span>
        <span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">ServiceInstance</span><span class="token punctuation">&gt;</span></span> instances <span class="token operator">=</span> discoveryClient<span class="token punctuation">.</span><span class="token function">getInstances</span><span class="token punctuation">(</span><span class="token string">&quot;product-service&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">int</span> index <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Random</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">nextInt</span><span class="token punctuation">(</span>instances<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">ServiceInstance</span> instance <span class="token operator">=</span> instances<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>index<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">String</span> host <span class="token operator">=</span> instance<span class="token punctuation">.</span><span class="token function">getHost</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">int</span> port <span class="token operator">=</span> instance<span class="token punctuation">.</span><span class="token function">getPort</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">String</span> url <span class="token operator">=</span> <span class="token string">&quot;http://&quot;</span> <span class="token operator">+</span> host <span class="token operator">+</span> <span class="token string">&quot;:&quot;</span> <span class="token operator">+</span> port <span class="token operator">+</span> <span class="token string">&quot;/products/&quot;</span> <span class="token operator">+</span> pid<span class="token punctuation">;</span>
        
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;从nacos中获取的url地址：&quot;</span> <span class="token operator">+</span> url<span class="token punctuation">)</span><span class="token punctuation">;</span>
        
        <span class="token class-name">Product</span> product <span class="token operator">=</span> restTemplate<span class="token punctuation">.</span><span class="token function">getForObject</span><span class="token punctuation">(</span>url<span class="token punctuation">,</span> <span class="token class-name">Product</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        order<span class="token punctuation">.</span><span class="token function">setPid</span><span class="token punctuation">(</span>pid<span class="token punctuation">)</span><span class="token punctuation">;</span>
        order<span class="token punctuation">.</span><span class="token function">setProductName</span><span class="token punctuation">(</span>product<span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        order<span class="token punctuation">.</span><span class="token function">setProductPrice</span><span class="token punctuation">(</span>product<span class="token punctuation">.</span><span class="token function">getPrice</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">//用户</span>
        order<span class="token punctuation">.</span><span class="token function">setUid</span><span class="token punctuation">(</span><span class="token number">1L</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        order<span class="token punctuation">.</span><span class="token function">setUsername</span><span class="token punctuation">(</span><span class="token string">&quot;dafei&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        order<span class="token punctuation">.</span><span class="token function">setNumber</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>order<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">super</span><span class="token punctuation">.</span><span class="token function">save</span><span class="token punctuation">(</span>order<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> order<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br><span class="line-number">48</span><br><span class="line-number">49</span><br><span class="line-number">50</span><br><span class="line-number">51</span><br><span class="line-number">52</span><br></div></div></li> <li><p>启动两个服务提供者和一个服务消费者，多访问几次消费者测试效果</p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20201029103519605.png" alt="image-20201029103519605"></strong></p></li></ol> <h3 id="_5-4-基于ribbon实现负载均衡"><a href="#_5-4-基于ribbon实现负载均衡" class="header-anchor">#</a> 5.4 基于Ribbon实现负载均衡</h3> <p>方案3 负载均衡方式存在问题：编写太麻烦了，有没有更加优雅的方式呢？答案是yes：<strong>Ribbon组件</strong></p> <p><strong>Ribbon</strong>是Spring Cloud的一个组件， 它可以让我们使用一个注解就能轻松的搞定负载均衡</p> <ol><li><p><strong>在RestTemplate 的生成方法上添加@LoadBalanced注解</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@Bean</span>
<span class="token annotation punctuation">@LoadBalanced</span>
<span class="token keyword">public</span> <span class="token class-name">RestTemplate</span> <span class="token function">restTemplate</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
	<span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">RestTemplate</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div></li> <li><p><strong>修改OrderServiceImpl服务调用的方法</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@Service</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">OrderServiceImpl</span> <span class="token keyword">extends</span> <span class="token class-name">ServiceImpl</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">OrderMapper</span><span class="token punctuation">,</span> <span class="token class-name">Order</span><span class="token punctuation">&gt;</span></span> <span class="token keyword">implements</span> <span class="token class-name">IOrderService</span> <span class="token punctuation">{</span>

    <span class="token annotation punctuation">@Autowired</span>
    <span class="token keyword">private</span> <span class="token class-name">RestTemplate</span> restTemplate<span class="token punctuation">;</span>

    <span class="token annotation punctuation">@Autowired</span>
    <span class="token keyword">private</span> <span class="token class-name">DiscoveryClient</span> discoveryClient<span class="token punctuation">;</span>

    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">public</span> <span class="token class-name">Order</span> <span class="token function">createOrder</span><span class="token punctuation">(</span><span class="token class-name">Long</span> pid<span class="token punctuation">,</span> <span class="token class-name">Long</span> uid<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">Order</span> order <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Order</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//商品</span>
        <span class="token comment">//方案1：通过restTemplate方式</span>
        <span class="token comment">//String url  = &quot;http://localhost:8081/products/&quot; + pid;</span>
        <span class="token comment">//Product product = restTemplate.getForObject(url, Product.class);</span>


        <span class="token comment">//方案2：使用注册中心api方式-discoveryClient</span>
        <span class="token comment">//List&lt;ServiceInstance&gt; instances = discoveryClient.getInstances(&quot;product-service&quot;);</span>
        <span class="token comment">//ServiceInstance instance = instances.get(0);  //当前只有一个</span>
        <span class="token comment">//String host = instance.getHost();</span>
        <span class="token comment">//int port = instance.getPort();</span>
        <span class="token comment">//String url = &quot;http://&quot; + host + &quot;:&quot; + port + &quot;/products/&quot; + pid;</span>
        <span class="token comment">//Product product = restTemplate.getForObject(url, Product.class);</span>

        <span class="token comment">//方案3：使用注册中心api方式-discoveryClient--带负载均衡</span>
        <span class="token comment">//List&lt;ServiceInstance&gt; instances = discoveryClient.getInstances(&quot;product-service&quot;);</span>
        <span class="token comment">//int index = new Random().nextInt(instances.size());</span>
        <span class="token comment">//ServiceInstance instance = instances.get(index);</span>
        <span class="token comment">//String host = instance.getHost();</span>
        <span class="token comment">//int port = instance.getPort();</span>
        <span class="token comment">//String url = &quot;http://&quot; + host + &quot;:&quot; + port + &quot;/products/&quot; + pid;</span>
        <span class="token comment">//System.out.println(&quot;从nacos中获取的url地址：&quot; + url);</span>
        <span class="token comment">//Product product = restTemplate.getForObject(url, Product.class);</span>

        <span class="token comment">//方案4：使用Ribbon方式--带负载均衡</span>
        <span class="token class-name">String</span> url <span class="token operator">=</span>  <span class="token string">&quot;http://product-service/products/&quot;</span> <span class="token operator">+</span> pid<span class="token punctuation">;</span>
        <span class="token class-name">Product</span> product <span class="token operator">=</span> restTemplate<span class="token punctuation">.</span><span class="token function">getForObject</span><span class="token punctuation">(</span>url<span class="token punctuation">,</span> <span class="token class-name">Product</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        order<span class="token punctuation">.</span><span class="token function">setPid</span><span class="token punctuation">(</span>pid<span class="token punctuation">)</span><span class="token punctuation">;</span>
        order<span class="token punctuation">.</span><span class="token function">setProductName</span><span class="token punctuation">(</span>product<span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        order<span class="token punctuation">.</span><span class="token function">setProductPrice</span><span class="token punctuation">(</span>product<span class="token punctuation">.</span><span class="token function">getPrice</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">//用户</span>
        order<span class="token punctuation">.</span><span class="token function">setUid</span><span class="token punctuation">(</span><span class="token number">1L</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        order<span class="token punctuation">.</span><span class="token function">setUsername</span><span class="token punctuation">(</span><span class="token string">&quot;dafei&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        order<span class="token punctuation">.</span><span class="token function">setNumber</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>order<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">super</span><span class="token punctuation">.</span><span class="token function">save</span><span class="token punctuation">(</span>order<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> order<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br><span class="line-number">48</span><br><span class="line-number">49</span><br><span class="line-number">50</span><br><span class="line-number">51</span><br><span class="line-number">52</span><br><span class="line-number">53</span><br></div></div></li> <li><p><strong>为了更直观看到请求是进行负载均衡了，我们修改一下ProductController代码</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@RestController</span>
<span class="token annotation punctuation">@RequestMapping</span><span class="token punctuation">(</span><span class="token string">&quot;products&quot;</span><span class="token punctuation">)</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">ProductController</span> <span class="token punctuation">{</span>
    <span class="token annotation punctuation">@Autowired</span>
    <span class="token keyword">private</span> <span class="token class-name">IProductService</span> productService<span class="token punctuation">;</span>

    <span class="token annotation punctuation">@Value</span><span class="token punctuation">(</span><span class="token string">&quot;${server.port}&quot;</span><span class="token punctuation">)</span>
    <span class="token keyword">private</span> <span class="token class-name">String</span> port<span class="token punctuation">;</span>

    <span class="token annotation punctuation">@GetMapping</span><span class="token punctuation">(</span><span class="token string">&quot;/{pid}&quot;</span><span class="token punctuation">)</span>
    <span class="token keyword">public</span> <span class="token class-name">Product</span> <span class="token function">findById</span><span class="token punctuation">(</span><span class="token annotation punctuation">@PathVariable</span> <span class="token class-name">Long</span> pid<span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token class-name">Product</span> product <span class="token operator">=</span> productService<span class="token punctuation">.</span><span class="token function">getById</span><span class="token punctuation">(</span>pid<span class="token punctuation">)</span><span class="token punctuation">;</span>
        product<span class="token punctuation">.</span><span class="token function">setName</span><span class="token punctuation">(</span>product<span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">&quot;—&quot;</span> <span class="token operator">+</span> port<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> product<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br></div></div></li> <li><p><strong>调用订单保存的方法，查看日志.</strong></p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20201029105040492.png" alt="image-20201029105040492"></p></li></ol> <h3 id="_5-5-ribbon支持的负载均衡策略"><a href="#_5-5-ribbon支持的负载均衡策略" class="header-anchor">#</a> 5.5 Ribbon支持的负载均衡策略</h3> <p>默认情况下，Ribbon 采用ZoneAvoidanceRule的策略，判断server所在区域的性能和server的可用性选择具体的server</p> <p>Ribbon内置了多种负载均衡策略，内部负载均衡的顶级接口为<strong>com.netflix.loadbalancer.IRule</strong> , 具体的负载策略如下图所示:</p> <table><thead><tr><th>策略名</th> <th>策略描述</th> <th>实现说明</th></tr></thead> <tbody><tr><td>BestAvailableRule</td> <td>选择一个最小的并发请求的server</td> <td>逐个考察Server，如果Server被   了，则忽略，在选择其中ActiveRequestsCount最小的server</td></tr> <tr><td>AvailabilityFilteringRule</td> <td>先过滤掉故障实例，再选择并发较小的实例；</td> <td>使用一个AvailabilityPredicate来包含过滤server的逻辑，其实就就是检查status里记录的各个server的运行状态</td></tr> <tr><td>WeightedResponseTimeRule</td> <td>根据相应时间分配一个weight，相应时间越长，weight越小，被选中的可能性越低。</td> <td>一个后台线程定期的从status里面读取评价响应时间，为每个server计算一个weight。Weight的计算也比较简单responsetime 减去每个server自己平均的responsetime是server的权</td></tr> <tr><td>RetryRule</td> <td>对选定的负载均衡策略机上重试机制。</td> <td>在一个配置时间段内当选择server不成功，则一直尝试使用subRule的方式选择一个可用的server</td></tr> <tr><td>RoundRobinRule</td> <td>轮询方式轮询选择server</td> <td>轮询index，选择index对应位置的server</td></tr> <tr><td>RandomRule</td> <td>随机选择一个server</td> <td>在index上随机，选择index对应位置的server</td></tr> <tr><td>ZoneAvoidanceRule(默认)</td> <td>复合判断server所在区域的性能和server的可用性选择server</td> <td>使用ZoneAvoidancePredicate和AvailabilityPredicate来判断是否选择某个server，前一个判断判定一个zone的运行性能是否可用，剔除不可用的zone（的所有server），AvailabilityPredicate用于过滤掉连接数过多的Server。</td></tr></tbody></table> <p>我们可以通过修改配置来调整Ribbon的负载均衡策略，在order-server项目的application.yml中增加如下配置:</p> <div class="language-yaml line-numbers-mode"><pre class="language-yaml"><code><span class="token key atrule">product-service</span><span class="token punctuation">:</span> <span class="token comment">## 调用的提供者的名称</span>
  <span class="token key atrule">ribbon</span><span class="token punctuation">:</span>
    <span class="token key atrule">NFLoadBalancerRuleClassName</span><span class="token punctuation">:</span> com.netflix.loadbalancer.RandomRule
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h2 id="六、远程调用feign"><a href="#六、远程调用feign" class="header-anchor">#</a> 六、远程调用Feign</h2> <p>前面使用Ribbon方式实现负载均衡版的远程调用，实现没问题，但是每次都需要手动拼接URL，有点小麻烦，有没有更加优雅的方式呢？答案是yes：<strong>使用Feign</strong></p> <h3 id="_6-1-什么是feign"><a href="#_6-1-什么是feign" class="header-anchor">#</a> 6.1 什么是Feign</h3> <p>​	Feign是Spring Cloud提供的一个声明式的伪Http客户端， 它使得调用远程服务就像调用本地服务一样简单， 只需要创建一个接口并添加一个注解即可。</p> <p>​	Nacos很好的兼容了Feign， Feign默认集成了 Ribbon， 所以在Nacos下使用Fegin默认就实现了负载均衡的效果。</p> <h3 id="_6-2-订单微服务集成feign"><a href="#_6-2-订单微服务集成feign" class="header-anchor">#</a> 6.2 订单微服务集成Feign</h3> <ol><li><p><strong>在shop-order-server项目的pom文件加入Fegin的依赖 （注意在服务调用方加）</strong></p> <div class="language-xml line-numbers-mode"><pre class="language-xml"><code><span class="token comment">&lt;!--fegin组件--&gt;</span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">&gt;</span></span>
	<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>org.springframework.cloud<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
	<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>spring-cloud-starter-openfeign<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div></li> <li><p><strong>在启动类OrderServer.java上添加Fegin的扫描注解,注意扫描路径(默认扫描当前包及其子包)</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@SpringBootApplication</span>
<span class="token annotation punctuation">@MapperScan</span><span class="token punctuation">(</span><span class="token string">&quot;cn.wolfcode.mapper&quot;</span><span class="token punctuation">)</span>
<span class="token annotation punctuation">@EnableDiscoveryClient</span>
<span class="token annotation punctuation">@EnableFeignClients</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">OrderServer</span> <span class="token punctuation">{</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">SpringApplication</span><span class="token punctuation">.</span><span class="token function">run</span><span class="token punctuation">(</span><span class="token class-name">OrderServer</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">,</span>args<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div></li> <li><p><strong>在shop-order-server项目中新增接口ProductFeignApi</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@FeignClient</span><span class="token punctuation">(</span>name <span class="token operator">=</span> <span class="token string">&quot;product-service&quot;</span><span class="token punctuation">)</span>
<span class="token keyword">public</span> <span class="token keyword">interface</span> <span class="token class-name">IProductFeginService</span> <span class="token punctuation">{</span>
    <span class="token annotation punctuation">@GetMapping</span><span class="token punctuation">(</span><span class="token string">&quot;/products/{pid}&quot;</span><span class="token punctuation">)</span>
    <span class="token class-name">Product</span> <span class="token function">get</span><span class="token punctuation">(</span><span class="token annotation punctuation">@PathVariable</span><span class="token punctuation">(</span><span class="token string">&quot;pid&quot;</span><span class="token punctuation">)</span> <span class="token class-name">Long</span> pid<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div></li> <li><p><strong>修改OrderServiceImpl.java的远程调用方法</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@Service</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">OrderServiceImpl</span> <span class="token keyword">extends</span> <span class="token class-name">ServiceImpl</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">OrderMapper</span><span class="token punctuation">,</span> <span class="token class-name">Order</span><span class="token punctuation">&gt;</span></span> <span class="token keyword">implements</span> <span class="token class-name">IOrderService</span> <span class="token punctuation">{</span>

    <span class="token annotation punctuation">@Autowired</span>
    <span class="token keyword">private</span> <span class="token class-name">RestTemplate</span> restTemplate<span class="token punctuation">;</span>

    <span class="token annotation punctuation">@Autowired</span>
    <span class="token keyword">private</span> <span class="token class-name">DiscoveryClient</span> discoveryClient<span class="token punctuation">;</span>

    <span class="token annotation punctuation">@Autowired</span>
    <span class="token keyword">private</span> <span class="token class-name">IProductFeginService</span> productFeginService<span class="token punctuation">;</span>

    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">public</span> <span class="token class-name">Order</span> <span class="token function">createOrder</span><span class="token punctuation">(</span><span class="token class-name">Long</span> pid<span class="token punctuation">,</span> <span class="token class-name">Long</span> uid<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">Order</span> order <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Order</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//商品</span>
        <span class="token comment">//方案1：通过restTemplate方式</span>
        <span class="token comment">//String url  = &quot;http://localhost:8081/products/&quot; + pid;</span>
        <span class="token comment">//Product product = restTemplate.getForObject(url, Product.class);</span>


        <span class="token comment">//方案2：使用注册中心api方式-discoveryClient</span>
        <span class="token comment">//List&lt;ServiceInstance&gt; instances = discoveryClient.getInstances(&quot;product-service&quot;);</span>
        <span class="token comment">//ServiceInstance instance = instances.get(0);  //当前只有一个</span>
        <span class="token comment">//String host = instance.getHost();</span>
        <span class="token comment">//int port = instance.getPort();</span>
        <span class="token comment">//String url = &quot;http://&quot; + host + &quot;:&quot; + port + &quot;/products/&quot; + pid;</span>
        <span class="token comment">//Product product = restTemplate.getForObject(url, Product.class);</span>

        <span class="token comment">//方案3：使用注册中心api方式-discoveryClient--带负载均衡</span>
        <span class="token comment">//List&lt;ServiceInstance&gt; instances = discoveryClient.getInstances(&quot;product-service&quot;);</span>
        <span class="token comment">//int index = new Random().nextInt(instances.size());</span>
        <span class="token comment">//ServiceInstance instance = instances.get(index);</span>
        <span class="token comment">//String host = instance.getHost();</span>
        <span class="token comment">//int port = instance.getPort();</span>
        <span class="token comment">//String url = &quot;http://&quot; + host + &quot;:&quot; + port + &quot;/products/&quot; + pid;</span>
        <span class="token comment">//System.out.println(&quot;从nacos中获取的url地址：&quot; + url);</span>
        <span class="token comment">//Product product = restTemplate.getForObject(url, Product.class);</span>

        <span class="token comment">//方案4：使用Ribbon方式--带负载均衡</span>
        <span class="token comment">//String url =  &quot;http://product-service/products/&quot; + pid;</span>
        <span class="token comment">//Product product = restTemplate.getForObject(url, Product.class);</span>

        <span class="token comment">//方案5：使用fegin接口--带负载均衡</span>
        <span class="token class-name">Product</span> product <span class="token operator">=</span> productFeginService<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>pid<span class="token punctuation">)</span><span class="token punctuation">;</span>

        order<span class="token punctuation">.</span><span class="token function">setPid</span><span class="token punctuation">(</span>pid<span class="token punctuation">)</span><span class="token punctuation">;</span>
        order<span class="token punctuation">.</span><span class="token function">setProductName</span><span class="token punctuation">(</span>product<span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        order<span class="token punctuation">.</span><span class="token function">setProductPrice</span><span class="token punctuation">(</span>product<span class="token punctuation">.</span><span class="token function">getPrice</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">//用户</span>
        order<span class="token punctuation">.</span><span class="token function">setUid</span><span class="token punctuation">(</span><span class="token number">1L</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        order<span class="token punctuation">.</span><span class="token function">setUsername</span><span class="token punctuation">(</span><span class="token string">&quot;dafei&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        order<span class="token punctuation">.</span><span class="token function">setNumber</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>order<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">super</span><span class="token punctuation">.</span><span class="token function">save</span><span class="token punctuation">(</span>order<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> order<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br><span class="line-number">48</span><br><span class="line-number">49</span><br><span class="line-number">50</span><br><span class="line-number">51</span><br><span class="line-number">52</span><br><span class="line-number">53</span><br><span class="line-number">54</span><br><span class="line-number">55</span><br><span class="line-number">56</span><br><span class="line-number">57</span><br><span class="line-number">58</span><br><span class="line-number">59</span><br></div></div></li> <li><p><strong>重启订单服务，并验证.</strong></p></li></ol> <h3 id="_6-3-feign接口使用原理"><a href="#_6-3-feign接口使用原理" class="header-anchor">#</a> 6.3 Feign接口使用原理</h3> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20221014094339989.png" alt="image-20221014094339989"></p> <h3 id="_6-4-feign接口定义注意要点"><a href="#_6-4-feign接口定义注意要点" class="header-anchor">#</a> 6.4 Feign接口定义注意要点</h3> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@FeignClient</span><span class="token punctuation">(</span>name <span class="token operator">=</span> <span class="token string">&quot;product-service&quot;</span><span class="token punctuation">)</span>
<span class="token keyword">public</span> <span class="token keyword">interface</span> <span class="token class-name">IProductFeginService</span> <span class="token punctuation">{</span>
    <span class="token annotation punctuation">@GetMapping</span><span class="token punctuation">(</span><span class="token string">&quot;/products/{pid}&quot;</span><span class="token punctuation">)</span>
    <span class="token class-name">Product</span> <span class="token function">get</span><span class="token punctuation">(</span><span class="token annotation punctuation">@PathVariable</span><span class="token punctuation">(</span><span class="token string">&quot;pid&quot;</span><span class="token punctuation">)</span> <span class="token class-name">Long</span> pid<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p><strong>Fegin接口使用主要点：</strong></p> <p>1&gt;@FeignClient 中name为服务提供者在nacos上注册的服务名， 否则报错</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token class-name">Load</span> balancer does not have available server <span class="token keyword">for</span> client<span class="token operator">:</span>xxxx<span class="token operator">-</span>service
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>2&gt;@GetMapping(&quot;/products/{pid}&quot;) 指定接口路径，必须跟服务提供者提供接口url一样，否则报错</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token class-name"><span class="token namespace">feign<span class="token punctuation">.</span></span>FeignException</span>$<span class="token class-name">NotFound</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token number">404</span><span class="token punctuation">]</span> during <span class="token punctuation">[</span><span class="token constant">GET</span><span class="token punctuation">]</span> <span class="token keyword">to</span> <span class="token punctuation">[</span>http<span class="token operator">:</span><span class="token operator">/</span><span class="token operator">/</span>xxx<span class="token operator">-</span>service<span class="token operator">/</span>xxx<span class="token punctuation">]</span> 
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>3&gt; 定义接口参数：如果使用了参数路径方式访问，需要使用@PathVariable(&quot;pid&quot;) 明确指定路径参数，否则报错</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token class-name"><span class="token namespace">feign<span class="token punctuation">.</span></span>FeignException</span>$<span class="token class-name">NotFound</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token number">404</span><span class="token punctuation">]</span> during <span class="token punctuation">[</span><span class="token constant">GET</span><span class="token punctuation">]</span> <span class="token keyword">to</span> <span class="token punctuation">[</span>http<span class="token operator">:</span><span class="token operator">/</span><span class="token operator">/</span>xxx<span class="token operator">-</span>service<span class="token operator">/</span>xxx<span class="token punctuation">]</span> 
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>4&gt;定义接口参数：如果使用普通方式访问，参数需要使用@RequestParam标记，否则报错</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token class-name"><span class="token namespace">feign<span class="token punctuation">.</span></span>FeignException</span>$<span class="token class-name">MethodNotAllowed</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token number">405</span><span class="token punctuation">]</span> during <span class="token punctuation">[</span><span class="token constant">GET</span><span class="token punctuation">]</span> <span class="token keyword">to</span> <span class="token punctuation">[</span>http<span class="token operator">:</span><span class="token operator">/</span><span class="token operator">/</span>xxxx<span class="token operator">-</span>service<span class="token operator">/</span>xxxx<span class="token operator">?</span>xxx<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>5&gt;定义接口参数：如果是对象参数，参数需要使用@RequestBody标记（注意fegin接口，controller接口都要），否则报错</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code>参数无法获取
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>6&gt;定义接口参数：如果是对象，可以使用@SpringQueryMap替换上面的@RequestBody</p> <p>7&gt;定义接口参数：如果需要进行文件上传，需要使用@RequestPart注解标记</p> <p>8&gt;Feign接口调用默认连接时间是1s，如果电脑较慢，开发中可以配置长一点时间</p> <p>注意：后面学sentinel 时候，不要配置，会影响观测效果</p> <div class="language-yaml line-numbers-mode"><pre class="language-yaml"><code><span class="token key atrule">feign</span><span class="token punctuation">:</span>
  <span class="token key atrule">client</span><span class="token punctuation">:</span>
    <span class="token key atrule">config</span><span class="token punctuation">:</span>
      <span class="token key atrule">default</span><span class="token punctuation">:</span>
        <span class="token key atrule">connectTimeout</span><span class="token punctuation">:</span> <span class="token number">5000</span>  <span class="token comment">#连接时间，单位毫秒</span>
        <span class="token key atrule">readTimeout</span><span class="token punctuation">:</span> <span class="token number">5000</span>     <span class="token comment">#操作时间</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><h2 id="七、服务熔断降级-sentinel"><a href="#七、服务熔断降级-sentinel" class="header-anchor">#</a> 七、服务熔断降级 Sentinel</h2> <p>微服务架构应用设计目的为了应对高并发环境，那问题来，高并发环境会带来啥问题，微服务架构设计时需要考虑啥预防性操作？</p> <h3 id="_7-1-高并发带来的问题"><a href="#_7-1-高并发带来的问题" class="header-anchor">#</a> 7.1 高并发带来的问题</h3> <p>在微服务架构中，我们将业务拆分成一个个的服务，服务与服务之间可以相互调用，但是由于网络原因或者自身的原因，服务并不能保证服务的100%可用，如果单个服务出现问题，调用这个服务就会出现网络延迟，此时若有大量的网络涌入，会形成任务堆积，最终导致服务瘫痪。</p> <p><strong>接下来，我们来模拟一个高并发的场景</strong></p> <ol><li><p><strong>在订单服务中新建SentinelController.java</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@RestController</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">SentinelController</span> <span class="token punctuation">{</span>
    <span class="token annotation punctuation">@RequestMapping</span><span class="token punctuation">(</span><span class="token string">&quot;/sentinel1&quot;</span><span class="token punctuation">)</span>
    <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">sentinel1</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token comment">//模拟一次网络延时</span>
        <span class="token keyword">try</span> <span class="token punctuation">{</span>
            <span class="token class-name">TimeUnit</span><span class="token punctuation">.</span><span class="token constant">SECONDS</span><span class="token punctuation">.</span><span class="token function">sleep</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">InterruptedException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token keyword">return</span> <span class="token string">&quot;sentinel1&quot;</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token annotation punctuation">@RequestMapping</span><span class="token punctuation">(</span><span class="token string">&quot;/sentinel2&quot;</span><span class="token punctuation">)</span>
    <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">sentinel2</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token string">&quot;测试高并发下的问题&quot;</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br></div></div></li> <li><p><strong>修改配置文件中tomcat的并发数</strong></p> <div class="language-yaml line-numbers-mode"><pre class="language-yaml"><code><span class="token key atrule">server</span><span class="token punctuation">:</span>
  <span class="token key atrule">port</span><span class="token punctuation">:</span> <span class="token number">8091</span>
  <span class="token key atrule">tomcat</span><span class="token punctuation">:</span>
    <span class="token key atrule">threads</span><span class="token punctuation">:</span>
      <span class="token key atrule">max</span><span class="token punctuation">:</span> <span class="token number">10</span> <span class="token comment">#tomcat的最大并发值修改为10,</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div></li> <li><p><strong>接下来使用压测工具,对请求进行压力测试</strong></p> <p>下载地址https://jmeter.apache.org/</p> <ul><li><p>第一步：修改配置，并启动软件</p> <p>进入bin目录,修改jmeter.properties文件中的语言支持为language=zh_CN，然后点击jmeter.bat</p> <p>启动软件。</p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20201029115836172.png" alt="image-20201029115836172"></strong></p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20201029115935101.png" alt="image-20201029115935101"></strong></p></li> <li><p>第二步：添加线程组</p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20201029120234401.png" alt="image-20201029120234401"></strong></p></li> <li><p>第三步：配置线程并发数</p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20201029124003847.png" alt="image-20201029124003847"></strong></p></li> <li><p>第四步：添加Http请求</p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20201029122851926.png" alt="image-20201029122851926"></strong></p></li> <li><p>第五步：配置取样，并启动测试</p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20201029123042028.png" alt="image-20201029123042028"></strong></p></li></ul> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20221014115054184.png" alt="image-20221014115054184"></p> <ul><li>第六步：访问 http://localhost:8091/sentinel2 观察结果</li></ul> <p><strong>结论</strong>:此时会发现, 由于sentinel1方法囤积了大量请求, 导致sentinel2方法的访问出现了问题，这就是服务雪</p> <p>崩的雏形。</p> <h3 id="_6-2-服务器雪崩效应"><a href="#_6-2-服务器雪崩效应" class="header-anchor">#</a> 6.2 服务器雪崩效应</h3> <p>前面同一个服务器不同接口间，当一个接口高频访问耗费完资源会影响到其他接口正常使用，如果将这个场景扩展到不同微服务间会怎么样呢？答案：<strong>服务器雪崩</strong>。</p> <p>​	在分布式系统中,由于网络原因或自身的原因,服务一般无法保证 100% 可用。如果一个服务出现了问题，调用这个服务就会出现线程阻塞的情况，此时若有大量的请求涌入，就会出现多条线程阻塞等待，进而导致服务瘫痪。</p> <p>​	由于服务与服务之间的依赖性，故障会传播，会对整个微服务系统造成灾难性的严重后果，这就是服务故障的 “<strong>雪崩效应</strong>” 。</p> <ul><li><p>情景1: 微服务之间相互调用,关系复杂,正常情况如下图所示:</p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20201029125354604.png" alt="image-20201029125354604"></strong></p></li> <li><p>情景2：某个时刻，服务A挂了，服务B和服务C依然在调用服务A</p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20201029135409624.png" alt="image-20201029135409624"></strong></p></li> <li><p>情景3:由于服务A挂了,导致服务C和服务B无法得到服务A的响应,这时候服务C和服务B由于大量线程积压,最终导致服务C和服务B挂掉.</p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20201029140115122.png" alt="image-20201029140115122"></strong></p></li> <li><p>情景4: 相同道理,由于服务之间有关联，所以会导致整个调用链上的所有服务都挂掉.</p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20201029140259527.png" alt="image-20201029140259527"></strong></p></li></ul> <p>​	服务器的雪崩效应其实就是由于某个微小的服务挂了,导致整一大片的服务都不可用.类似生活中的雪崩效应,由于落下的最后一片雪花引发了雪崩的情况.</p> <p>​	雪崩发生的原因多种多样，有不合理的容量设计，或者是高并发下某一个方法响应变慢，亦或是某台机器的资源耗尽。我们无法完全杜绝雪崩源头的发生，只有做好足够的容错，保证在一个服务发生问题，不会影响到其它服务的正常运行。</p></li></ol> <h3 id="_7-2-常见容错方案"><a href="#_7-2-常见容错方案" class="header-anchor">#</a> 7.2 常见容错方案</h3> <p>​	要防止雪崩的扩散，我们就要做好服务的容错，容错说白了就是保护自己不被猪队友拖垮的一些措施, 下面介绍常见的服务容错思路和组件。</p> <p><strong>常见的容错思路</strong></p> <p>常见的容错思路有<strong>隔离、超时、限流、熔断、降级</strong>这几种，下面分别介绍一下。</p> <ul><li><p><strong>隔离机制</strong>: 比如服务A内限制有100个线程, 现在服务A可能会调用服务B,服务C,服务D.我们在服务A进行远程调用的时候,给不同的服务分配固定的线程,不会把所有线程都分配给某个微服务. 比如调用服务B分配30个线程,调用服务C分配30个线程，调用服务D分配40个线程. 这样进行资源的隔离，保证即使下游某个服务挂了，也不至于把服务A的线程消耗完。比如服务B挂了，这时候最多只会占用服务A的30个线程,服务A还有70个线程可以调用服务C和服务D.</p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20201029142100450.png" alt="image-20201029142100450"></strong></p></li> <li><p><strong>超时机制</strong>: 在上游服务调用下游服务的时候，设置一个最大响应时间，如果超过这个时间，下游未作出反应，</p> <p>就断开请求，释放掉线程。</p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20201029143237828.png" alt="image-20201029143237828"></strong></p></li> <li><p><strong>限流机制</strong>: 限流就是限制系统的输入和输出流量已达到保护系统的目的。为了保证系统的稳固运行,一旦达到</p> <p>的需要限制的阈值,就需要限制流量并采取少量措施以完成限制流量的目的。</p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20201029143206491.png" alt="image-20201029143206491"></strong></p></li> <li><p><strong>熔断机制</strong>: 在互联网系统中，当下游服务因访问压力过大而响应变慢或失败，上游服务为了保护系统整</p> <p>体的可用性，可以暂时切断对下游服务的调用。这种牺牲局部，保全整体的措施就叫做熔断。</p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20201029143128555.png" alt="image-20201029143128555"></strong></p> <p>服务熔断一般有三种状态：</p> <ul><li>熔断关闭状态（Closed）</li></ul> <p>服务没有故障时，熔断器所处的状态，对调用方的调用不做任何限制</p> <ul><li>熔断开启状态（Open）</li></ul> <p>后续对该服务接口的调用不再经过网络，直接执行本地的fallback方法</p> <ul><li>半熔断状态（Half-Open）</li></ul> <p>尝试恢复服务调用，允许有限的流量调用该服务，并监控调用成功率。如果成功率达到预</p> <p>期，则说明服务已恢复，进入熔断关闭状态；如果成功率仍旧很低，则重新进入熔断开启状</p> <p>态。</p></li> <li><p><strong>降级机制</strong>: 降级其实就是为服务提供一个兜底方案，一旦服务无法正常调用，就使用兜底方案。</p></li></ul> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20201029143456888.png" alt="image-20201029143456888"></strong></p> <h3 id="_7-3-常见的容错组件"><a href="#_7-3-常见的容错组件" class="header-anchor">#</a> 7.3 常见的容错组件</h3> <ul><li><p><strong>Hystrix</strong></p> <p>Hystrix是由Netflflix开源的一个延迟和容错库，用于隔离访问远程系统、服务或者第三方库，防止</p></li></ul> <p>级联失败，从而提升系统的可用性与容错性。</p> <ul><li><p><strong>Resilience4J</strong></p> <p>Resilicence4J一款非常轻量、简单，并且文档非常清晰、丰富的熔断工具，这也是Hystrix官方推</p></li></ul> <p>荐的替代产品。不仅如此，Resilicence4j还原生支持Spring Boot 1.x/2.x，而且监控也支持和</p> <p>prometheus等多款主流产品进行整合。</p> <ul><li><p><strong>Sentinel</strong></p> <p>Sentinel 是阿里巴巴开源的一款断路器实现，本身在阿里内部已经被大规模采用，非常稳定。</p></li></ul> <h3 id="_7-4-sentinel入门"><a href="#_7-4-sentinel入门" class="header-anchor">#</a> 7.4 Sentinel入门</h3> <h4 id="_7-4-1-什么是sentinel"><a href="#_7-4-1-什么是sentinel" class="header-anchor">#</a> 7.4.1 什么是Sentinel</h4> <p><strong>官网：https://sentinelguard.io/zh-cn/index.html</strong></p> <p>Sentinel (分布式系统的流量防卫兵) 是阿里开源的一套用于<strong>服务容错</strong>的综合性解决方案。它以流量</p> <p>为切入点, 从<strong>流量控制、熔断降级、系统负载保护</strong>等多个维度来保护服务的稳定性</p> <p>Sentinel 具有以下特征:</p> <ul><li><strong>丰富的应用场景</strong>：Sentinel 承接了阿里巴巴近 10 年的双十一大促流量的核心场景, 例如秒杀（即</li></ul> <p>突发流量控制在系统容量可以承受的范围）、消息削峰填谷、集群流量控制、实时熔断下游不可用</p> <p>应用等。</p> <ul><li><strong>完备的实时监控</strong>：Sentinel 提供了实时的监控功能。通过控制台可以看到接入应用的单台机器秒</li></ul> <p>级数据, 甚至 500 台以下规模的集群的汇总运行情况。</p> <ul><li><strong>广泛的开源生态</strong>：Sentinel 提供开箱即用的与其它开源框架/库的整合模块, 例如与 Spring</li></ul> <p>Cloud、Dubbo、gRPC 的整合。只需要引入相应的依赖并进行简单的配置即可快速地接入</p> <p>Sentinel。</p> <p><strong>Sentinel分为两个部分</strong>:</p> <ul><li>核心库（Java 客户端）不依赖任何框架/库,能够运行于所有 Java 运行时环境，同时对 Dubbo /</li></ul> <p>Spring Cloud 等框架也有较好的支持。</p> <ul><li>控制台（Dashboard）基于 Spring Boot 开发，打包后可以直接运行，不需要额外的 Tomcat 等</li></ul> <p>应用容器。</p> <h4 id="_7-4-2-订单微服务集成sentinel"><a href="#_7-4-2-订单微服务集成sentinel" class="header-anchor">#</a> 7.4.2 订单微服务集成Sentinel</h4> <p>为微服务集成Sentinel非常简单, 只需要加入Sentinel的依赖即可</p> <p>在shop-order-server项目的pom文件中添加如下依赖</p> <div class="language-xml line-numbers-mode"><pre class="language-xml"><code><span class="token comment">&lt;!--sentinel组件--&gt;</span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">&gt;</span></span>
	<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>com.alibaba.cloud<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
	<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>spring-cloud-starter-alibaba-sentinel<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><h4 id="_7-4-3-安装sentinel控制台"><a href="#_7-4-3-安装sentinel控制台" class="header-anchor">#</a> 7.4.3 安装Sentinel控制台</h4> <p>Sentinel 提供一个轻量级的控制台, 它提供机器发现、单机资源实时监控以及规则管理等功能。</p> <ol><li><p><strong>下载jar包 https://github.com/alibaba/Sentinel/releases</strong></p></li> <li><p><strong>启动控制台</strong></p> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code><span class="token comment">## 直接使用jar命令启动项目(控制台本身是一个SpringBoot项目) </span>
<span class="token function">java</span> <span class="token parameter variable">-Dserver.port</span><span class="token operator">=</span><span class="token number">8080</span> <span class="token parameter variable">-Dcsp.sentinel.dashboard.server</span><span class="token operator">=</span>localhost:8080 <span class="token parameter variable">-Dproject.name</span><span class="token operator">=</span>sentinel-dashboard <span class="token parameter variable">-jar</span> sentinel-dashboard-1.8.0.jar
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><div class="language-properties line-numbers-mode"><pre class="language-properties"><code><span class="token key attr-name">-Dserver.port</span><span class="token punctuation">=</span><span class="token value attr-value">8080 #用于指定 Sentinel 控制台端口为 8080，如若8080端口冲突，可使用 -Dserver.port=新端口 进行设置。。</span>
<span class="token key attr-name">-Dcsp.sentinel.dashboard.server</span><span class="token punctuation">=</span><span class="token value attr-value">localhost:8080 #指定控制台地址和端口，会自动向该地址发送心跳包。地址格式为：hostIp:port #配置成ocalhost:8080即监控自己</span>
<span class="token key attr-name">-Dproject.name</span><span class="token punctuation">=</span><span class="token value attr-value">sentinel-dashboard #指定Sentinel控制台程序显示的名称</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p><strong>这里注意：</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token comment">// 指定jdk版本</span>
set <span class="token class-name">Path</span><span class="token operator">=</span>jdk8以上版本\bin
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div></li> <li><p><strong>修改shop-order-server项目中的配置文件application.yml,新增如下配置:</strong></p> <div class="language-yaml line-numbers-mode"><pre class="language-yaml"><code><span class="token key atrule">spring</span><span class="token punctuation">:</span>
  <span class="token key atrule">cloud</span><span class="token punctuation">:</span>
    <span class="token key atrule">sentinel</span><span class="token punctuation">:</span> 
      <span class="token key atrule">transport</span><span class="token punctuation">:</span> 
        <span class="token key atrule">port</span><span class="token punctuation">:</span> <span class="token number">8719</span> <span class="token comment">#跟控制台交流的端口,随意指定一个未使用的端口即可,默认为8719</span>
        <span class="token key atrule">dashboard</span><span class="token punctuation">:</span> localhost<span class="token punctuation">:</span><span class="token number">8080</span> <span class="token comment">## 指定控制台服务的地址</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div></li> <li><p><strong>通过浏览器访问localhost:8080 进入控制台 ( 默认用户名密码是 sentinel/sentinel )</strong></p> <p>注意: 默认是没显示order-service的，需要访问几次接口，然后再刷新sentinel管控台才可以看到.</p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20201029151420936.png" alt="image-20201029151420936"></strong></p></li></ol> <h4 id="_7-4-4-实现一个接口的限流"><a href="#_7-4-4-实现一个接口的限流" class="header-anchor">#</a> 7.4.4 实现一个接口的限流</h4> <p><strong>第一步: 簇点链路---&gt;流控</strong></p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20201029151937258.png" alt="image-20201029151937258"></strong></p> <p><strong>第二步: 在单机阈值填写一个数值，表示每秒上限的请求数</strong></p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20201029152031334.png" alt="image-20201029152031334"></strong></p> <p><strong>第三步:通过控制台快速频繁访问, 观察效果</strong></p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20201029152401508.png" alt="image-20201029152401508"></strong></p> <h4 id="_7-4-5-sentinel容错的维度"><a href="#_7-4-5-sentinel容错的维度" class="header-anchor">#</a> 7.4.5 Sentinel容错的维度</h4> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20201029153848900.png" alt="image-20201029153848900"></strong></p> <p><strong>流量控制</strong>：流量控制在网络传输中是一个常用的概念，它用于调整网络包的数据。任意时间到来的请求往往是随机不可控的，而系统的处理能力是有限的。我们需要根据系统的处理能力对流量进行控制。</p> <p><strong>熔断降级</strong>：当检测到调用链路中某个资源出现不稳定的表现，例如请求响应时间长或异常比例升高的时候，则对这个资源的调用进行限制，让请求快速失败，避免影响到其它的资源而导致级联故障。</p> <p><strong>系统负载保护</strong>：Sentinel 同时提供系统维度的自适应保护能力。当系统负载较高的时候，如果还持续让请求进入可能会导致系统崩溃，无法响应。在集群环境下，会把本应这台机器承载的流量转发到其它的机器上去。如果这个时候其它的机器也处在一个边缘状态的时候，Sentinel 提供了对应的保护机制，让系统的入口流量和系统的负载达到一个平衡，保证系统在能力范围之内处理最多的请求。</p> <h4 id="_7-4-6-sentinel规则种类"><a href="#_7-4-6-sentinel规则种类" class="header-anchor">#</a> 7.4.6 Sentinel规则种类</h4> <p>Sentinel主要提供了这五种的流量控制，接下来我们每种都给同学们演示一下.</p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20201029160041081.png" alt="image-20201029160041081"></strong></p> <h4 id="_7-4-7-sentinel控制实现原理"><a href="#_7-4-7-sentinel控制实现原理" class="header-anchor">#</a> 7.4.7 Sentinel控制实现原理</h4> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/sentinel-slot-chain-architecture.png" alt="img"></p> <h3 id="_7-5-sentinel规则-流控"><a href="#_7-5-sentinel规则-流控" class="header-anchor">#</a> 7.5 Sentinel规则-流控</h3> <h4 id="_7-5-1-流控规则"><a href="#_7-5-1-流控规则" class="header-anchor">#</a> 7.5.1 流控规则</h4> <p>流量控制，其原理是监控应用流量的QPS(每秒查询率) 或并发线程数等指标，当达到指定的阈值时</p> <p>对流量进行控制，以避免被瞬时的流量高峰冲垮，从而保障应用的高可用性。</p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20201029160919062.png" alt="image-20201029160919062"></strong></p> <p><strong>资源名</strong>：唯一名称，默认是请求路径，可自定义</p> <p><strong>针对来源</strong>：指定对哪个微服务进行限流，默认指default，意思是不区分来源，全部限制</p> <p><strong>阈值类型/单机阈值</strong>：</p> <ul><li><p>QPS（每秒请求数量）: 当调用该接口的QPS达到阈值的时候，进行限流</p></li> <li><p>线程数：当调用该接口的线程数达到阈值的时候，进行限流</p></li></ul> <p><strong>是否集群</strong>：暂不需要集群</p> <h5 id="_7-5-1-1-qps流控"><a href="#_7-5-1-1-qps流控" class="header-anchor">#</a> 7.5.1.1 QPS流控</h5> <p>前面6.4.4案例就是演示的QPS流控</p> <h5 id="_7-5-1-2-线程数流控"><a href="#_7-5-1-2-线程数流控" class="header-anchor">#</a> 7.5.1.2 线程数流控</h5> <ol><li><p>删除掉之前的QPS流控，新增线程数流控</p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20201029162926422.png" alt="image-20201029162926422"></strong></p></li> <li><p>在Jmeter中新增线程</p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20201029163205306.png" alt="image-20201029163205306"></strong></p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20201029163233714.png" alt="image-20201029163233714"></strong></p></li> <li><p>访问 http://localhost:8091/sentinel2 会发现已经被限流</p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20201029163416823.png" alt="image-20201029163416823"></strong></p></li></ol> <h4 id="_7-5-2-流控模式"><a href="#_7-5-2-流控模式" class="header-anchor">#</a> 7.5.2 流控模式</h4> <p>点击上面设置流控规则的<strong>编辑</strong>按钮，然后在编辑页面点击<strong>高级选项</strong>，会看到有流控模式一栏。</p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20201029164212597.png" alt="image-20201029164212597"></strong></p> <p>sentinel共有三种流控模式，分别是：</p> <ul><li><p>直接（默认）：接口达到限流条件时，开启限流</p></li> <li><p>关联：当关联的资源达到限流条件时，开启限流 [适合做应用让步]</p></li> <li><p>链路：当从某个接口过来的资源达到限流条件时，开启限流</p></li></ul> <h5 id="_7-5-2-1-直接流控模式"><a href="#_7-5-2-1-直接流控模式" class="header-anchor">#</a> 7.5.2.1 直接流控模式</h5> <p>前面演示的案例就是这种.</p> <h5 id="_7-5-2-2-关联流控模式"><a href="#_7-5-2-2-关联流控模式" class="header-anchor">#</a> 7.5.2.2 关联流控模式</h5> <p>关联流控模式指的是，当指定接口关联的接口达到限流条件时，开启对指定接口开启限流。</p> <p><strong>场景</strong>:当两个资源之间具有资源争抢或者依赖关系的时候，这两个资源便具有了关联。比如对数据库同一个字段的读操作和写操作存在争抢，读的速度过高会影响写得速度，写的速度过高会影响读的速度。如果放任读写操作争抢资源，则争抢本身带来的开销会降低整体的吞吐量。可使用关联限流来避免具有关联关系的资源之间过度的争抢.</p> <ol><li><p><strong>在SentinelController.java中增加一个方法，重启订单服务</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@RequestMapping</span><span class="token punctuation">(</span><span class="token string">&quot;/sentinel3&quot;</span><span class="token punctuation">)</span>
<span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">sentinel3</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
	<span class="token keyword">return</span> <span class="token string">&quot;sentinel3&quot;</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div></li> <li><p><strong>配置限流规则, 将流控模式设置为关联，关联资源设置为的 /sentinel2</strong></p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20201029170800980.png" alt="image-20201029170800980"></strong></p></li> <li><p>通过postman软件向/sentinel2连续发送请求，注意QPS一定要大于3</p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20201029171026368.png" alt="image-20201029171026368"></strong></p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20201029171055757.png" alt="image-20201029171055757"></strong></p></li> <li><p><strong>访问/sentinel3,会发现已经被限流</strong></p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20201029171402061.png" alt="image-20201029171402061"></strong></p></li></ol> <h5 id="_7-5-2-3-链路流控模式"><a href="#_7-5-2-3-链路流控模式" class="header-anchor">#</a> 7.5.2.3 链路流控模式</h5> <p>链路流控模式指的是，当从某个接口过来的资源达到限流条件时，开启限流。</p> <ol><li><p><strong>在shop-order-server项目的application.yml文件中新增如下配置:</strong></p> <div class="language-yaml line-numbers-mode"><pre class="language-yaml"><code><span class="token key atrule">spring</span><span class="token punctuation">:</span>
  <span class="token key atrule">cloud</span><span class="token punctuation">:</span>
    <span class="token key atrule">sentinel</span><span class="token punctuation">:</span>
      <span class="token key atrule">web-context-unify</span><span class="token punctuation">:</span> <span class="token boolean important">false</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div></li> <li><p><strong>在shop-order-server项目中新增TraceServiceImpl.java</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">package</span> <span class="token namespace">cn<span class="token punctuation">.</span>wolfcode<span class="token punctuation">.</span>service<span class="token punctuation">.</span>impl</span><span class="token punctuation">;</span>
<span class="token annotation punctuation">@Service</span>
<span class="token annotation punctuation">@Slf4j</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">TraceServiceImpl</span> <span class="token punctuation">{</span>
    <span class="token annotation punctuation">@SentinelResource</span><span class="token punctuation">(</span>value <span class="token operator">=</span> <span class="token string">&quot;tranceService&quot;</span><span class="token punctuation">)</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">tranceService</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        log<span class="token punctuation">.</span><span class="token function">info</span><span class="token punctuation">(</span><span class="token string">&quot;调用tranceService方法&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div></li> <li><p><strong>在shop-order-server项目中新增TraceController.java</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">package</span> <span class="token namespace">cn<span class="token punctuation">.</span>wolfcode<span class="token punctuation">.</span>controller</span><span class="token punctuation">;</span>
<span class="token annotation punctuation">@RestController</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">TraceController</span> <span class="token punctuation">{</span>
    <span class="token annotation punctuation">@Autowired</span>
    <span class="token keyword">private</span> <span class="token class-name">TraceServiceImpl</span> traceService<span class="token punctuation">;</span>
    <span class="token annotation punctuation">@RequestMapping</span><span class="token punctuation">(</span><span class="token string">&quot;/trace1&quot;</span><span class="token punctuation">)</span>
    <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">trace1</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        traceService<span class="token punctuation">.</span><span class="token function">tranceService</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token string">&quot;trace1&quot;</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token annotation punctuation">@RequestMapping</span><span class="token punctuation">(</span><span class="token string">&quot;/trace2&quot;</span><span class="token punctuation">)</span>
    <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">trace2</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        traceService<span class="token punctuation">.</span><span class="token function">tranceService</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token string">&quot;trace2&quot;</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br></div></div></li> <li><p><strong>重新启动订单服务并添加链路流控规则</strong></p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20201029175454431.png" alt="image-20201029175454431"></strong></p></li> <li><p><strong>分别通过 /trace1 和 /trace2 访问, 发现/trace1没问题, /trace2的被限流了</strong></p></li></ol> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20201029175559035.png" alt="image-20201029175555499"></strong></p> <h4 id="_7-5-3-流控效果"><a href="#_7-5-3-流控效果" class="header-anchor">#</a> 7.5.3 流控效果</h4> <ul><li><p><strong>快速失败（默认）</strong>: 直接失败，抛出异常，不做任何额外的处理，是最简单的效果</p></li> <li><p><strong>Warm Up</strong>：它从开始阈值到最大QPS阈值会有一个缓冲阶段，一开始的阈值是最大QPS阈值的</p> <p>1/3，然后慢慢增长，直到最大阈值，适用于将突然增大的流量转换为缓步增长的场景。</p></li> <li><p><strong>排队等待</strong>：让请求以均匀的速度通过，单机阈值为每秒通过数量，其余的排队等待； 它还会让设</p> <p>置一个超时时间，当请求超过超时间时间还未处理，则会被丢弃。</p></li></ul> <h3 id="_7-6-sentinel规则-降级"><a href="#_7-6-sentinel规则-降级" class="header-anchor">#</a> 7.6 Sentinel规则-降级</h3> <p>降级规则就是设置当满足什么条件的时候，对服务进行降级。Sentinel提供了三个衡量条件：</p> <ul><li><strong>慢调用比例</strong>: 选择以慢调用比例作为阈值，需要设置允许的慢调用 RT（即最大的响应时间），请求的响应时间大于该值则统计为慢调用。当单位统计时长内请求数目大于设置的最小请求数目，并且慢调用的比例大于阈值，则接下来的熔断时长内请求会自动被熔断。经过熔断时长后熔断器会进入探测恢复状态（HALF-OPEN 状态），若接下来的一个请求响应时间小于设置的慢调用 RT 则结束熔断，若大于设置的慢调用 RT 则会再次被熔断。</li> <li><strong>异常比例</strong>: 当单位统计时长内请求数目大于设置的最小请求数目，并且异常的比例大于阈值，则接下来的熔断时长内请求会自动被熔断。经过熔断时长后熔断器会进入探测恢复状态（HALF-OPEN 状态），若接下来的一个请求成功完成（没有错误）则结束熔断，否则会再次被熔断。异常比率的阈值范围是 <code>[0.0, 1.0]</code>，代表 0% - 100%。</li> <li><strong>异常数</strong>：当单位统计时长内的异常数目超过阈值之后会自动进行熔断。经过熔断时长后熔断器会进入探测恢复状态（HALF-OPEN 状态），若接下来的一个请求成功完成（没有错误）则结束熔断，否则会再次被熔断。</li></ul> <h4 id="_7-6-1-慢调用比例案例"><a href="#_7-6-1-慢调用比例案例" class="header-anchor">#</a> 7.6.1 慢调用比例案例</h4> <ol><li><p><strong>在shop-order-server项目中新增FallBackController.java类,代码如下:</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">package</span> <span class="token namespace">cn<span class="token punctuation">.</span>wolfcode<span class="token punctuation">.</span>controller</span><span class="token punctuation">;</span>
<span class="token annotation punctuation">@RestController</span>
<span class="token annotation punctuation">@Slf4j</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">FallBackController</span> <span class="token punctuation">{</span>
    <span class="token annotation punctuation">@RequestMapping</span><span class="token punctuation">(</span><span class="token string">&quot;/fallBack1&quot;</span><span class="token punctuation">)</span>
    <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">fallBack1</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token keyword">try</span> <span class="token punctuation">{</span>
            log<span class="token punctuation">.</span><span class="token function">info</span><span class="token punctuation">(</span><span class="token string">&quot;fallBack1执行业务逻辑&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token comment">//模拟业务耗时</span>
            <span class="token class-name">TimeUnit</span><span class="token punctuation">.</span><span class="token constant">SECONDS</span><span class="token punctuation">.</span><span class="token function">sleep</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">InterruptedException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token keyword">return</span> <span class="token string">&quot;fallBack1&quot;</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br></div></div></li> <li><p><strong>新增降级规则:</strong></p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20201030094205307.png" alt="image-20201030094205307"></strong></p> <p>上面配置表示，如果在1S之内,有【超过1个的请求】且这些请求中【响应时间&gt;最大RT】的【请求数量比例&gt;10%】，就会触发熔断，在接下来的10s之内都不会调用真实方法，直接走降级方法。</p> <p>比如: 最大RT=900,比例阈值=0.1,熔断时长=10,最小请求数=10</p> <ul><li><p>情况1: 1秒内的有20个请求，只有10个请求响应时间&gt;900ms, 那慢调用比例=0.5，这种情况就会触发熔断</p></li> <li><p>情况2: 1秒内的有20个请求，只有1个请求响应时间&gt;900ms, 那慢调用比例=0.05，这种情况不会触发熔断</p></li> <li><p>情况3: 1秒内的有8个请求，只有6个请求响应时间&gt;900ms, 那慢调用比例=0.75，这种情况不会触发熔断，因为最小请求数这个条件没有满足.</p></li></ul> <p><strong>注意</strong>: 我们做实验的时候把最小请求数设置为1，因为在1秒内，手动操作很难在1s内发两个请求过去，所以要做出效果,最好把最小请求数设置为1。</p></li></ol> <h4 id="_7-6-2-异常比例案例"><a href="#_7-6-2-异常比例案例" class="header-anchor">#</a> 7.6.2 异常比例案例</h4> <ol><li><p><strong>在shop-order-server项目的FallBackController.java类新增fallBack2方法:</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>
<span class="token annotation punctuation">@RequestMapping</span><span class="token punctuation">(</span><span class="token string">&quot;/fallBack2&quot;</span><span class="token punctuation">)</span>
<span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">fallBack2</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
	log<span class="token punctuation">.</span><span class="token function">info</span><span class="token punctuation">(</span><span class="token string">&quot;fallBack2执行业务逻辑&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token comment">//模拟出现异常，异常比例为33%</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">++</span>i<span class="token operator">%</span><span class="token number">3</span><span class="token operator">==</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
		<span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">RuntimeException</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
	<span class="token keyword">return</span> <span class="token string">&quot;fallBack2&quot;</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div></li> <li><p><strong>新增降级规则:</strong></p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20201030100912417.png" alt="image-20201030100912417"></strong></p> <p>上面配置表示，在1s之内，,有【超过3个的请求】，异常比例30%的情况下，触发熔断，熔断时长为10s.</p></li></ol> <h4 id="_7-6-3-异常数案例"><a href="#_7-6-3-异常数案例" class="header-anchor">#</a> 7.6.3 异常数案例</h4> <ol><li><p><strong>在shop-order-server项目的FallBackController.java类新增fallBack3方法:</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@RequestMapping</span><span class="token punctuation">(</span><span class="token string">&quot;/fallBack3&quot;</span><span class="token punctuation">)</span>
<span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">fallBack3</span><span class="token punctuation">(</span><span class="token class-name">String</span> name<span class="token punctuation">)</span><span class="token punctuation">{</span>
	log<span class="token punctuation">.</span><span class="token function">info</span><span class="token punctuation">(</span><span class="token string">&quot;fallBack3执行业务逻辑&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">if</span><span class="token punctuation">(</span><span class="token string">&quot;wolfcode&quot;</span><span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span>name<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
		<span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">RuntimeException</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
	<span class="token keyword">return</span> <span class="token string">&quot;fallBack3&quot;</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div></li> <li><p><strong>新增降级规则</strong></p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20201030102109574.png" alt="image-20201030102109574"></strong></p> <p>上面配置表示，在1s之内，,有【超过3个的请求】，请求中超过2个请求出现异常就会触发熔断，熔断时长为10s</p></li></ol> <h3 id="_7-7-sentinel规则-热点"><a href="#_7-7-sentinel规则-热点" class="header-anchor">#</a> 7.7 Sentinel规则-热点</h3> <p>何为热点？热点即经常访问的数据。很多时候我们希望统计某个热点数据中访问频次最高的 Top K 数据，并对其访问进行限制。比如：</p> <ul><li>商品 ID 为参数，统计一段时间内最常购买的商品 ID 并进行限制</li> <li>用户 ID 为参数，针对一段时间内频繁访问的用户 ID 进行限制</li></ul> <p>热点参数限流会统计传入参数中的热点参数，并根据配置的限流阈值与模式，对包含热点参数的资源调用进行限流。热点参数限流可以看做是一种特殊的流量控制，仅对包含热点参数的资源调用生效。</p> <ol><li><p><strong>在shop-order-server项目中新增HotSpotController.java,代码如下:</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">package</span> <span class="token namespace">cn<span class="token punctuation">.</span>wolfcode<span class="token punctuation">.</span>controller</span><span class="token punctuation">;</span>
<span class="token annotation punctuation">@RestController</span>
<span class="token annotation punctuation">@Slf4j</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">HotSpotController</span> <span class="token punctuation">{</span>
    <span class="token annotation punctuation">@RequestMapping</span><span class="token punctuation">(</span><span class="token string">&quot;/hotSpot1&quot;</span><span class="token punctuation">)</span>
    <span class="token annotation punctuation">@SentinelResource</span><span class="token punctuation">(</span>value <span class="token operator">=</span> <span class="token string">&quot;hotSpot1&quot;</span><span class="token punctuation">)</span>
    <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">hotSpot1</span><span class="token punctuation">(</span><span class="token class-name">Long</span> productId<span class="token punctuation">)</span><span class="token punctuation">{</span>
        log<span class="token punctuation">.</span><span class="token function">info</span><span class="token punctuation">(</span><span class="token string">&quot;访问编号为:{}的商品&quot;</span><span class="token punctuation">,</span>productId<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token string">&quot;hotSpot1&quot;</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><p>注意:一定需要在请求方法上贴@SentinelResource直接，否则热点规则无效</p></li> <li><p><strong>新增热点规则:</strong></p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20201030104511784.png" alt="image-20201030104511784"></strong></p></li> <li><p><strong>在热点规则中编辑规则,在编辑之前一定要先访问一下/hotSpot1,不然参数规则无法新增.</strong></p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20201030104633745.png" alt="image-20201030104633745"></strong></p></li> <li><p><strong>新增参数规则:</strong></p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20201030104831352.png" alt="image-20201030104831352"></strong></p></li> <li><p><strong>点击保存，可以看到已经新增了参数规则.</strong></p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20201030104957789.png" alt="image-20201030104957789"></strong></p></li> <li><p><strong>访问测试</strong></p> <p>访问http://localhost:8091/hotSpot?productId=1 访问会降级</p> <p>访问http://localhost:8091/hotSpot?productId=2 访问不会降级</p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20201030105114878.png" alt="image-20201030105114878"></strong></p></li></ol> <h3 id="_7-8-sentinel规则-授权"><a href="#_7-8-sentinel规则-授权" class="header-anchor">#</a> 7.8 Sentinel规则-授权</h3> <p>很多时候，我们需要根据调用来源来判断该次请求是否允许放行，这时候可以使用 Sentinel 的来源访问控制（黑白名单控制）的功能。来源访问控制根据资源的请求来源（<code>origin</code>）限制资源是否通过，若配置白名单则只有请求来源位于白名单内时才可通过；若配置黑名单则请求来源位于黑名单时不通过，其余的请求通过。</p> <ol><li><p><strong>在shop-order-server中新建RequestOriginParserDefinition.java,定义请求来源如何获取</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@Component</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">RequestOriginParserDefinition</span> <span class="token keyword">implements</span> <span class="token class-name">RequestOriginParser</span> <span class="token punctuation">{</span>
    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">parseOrigin</span><span class="token punctuation">(</span><span class="token class-name">HttpServletRequest</span> request<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">/**
         *  定义从请求的什么地方获取来源信息
         *  比如我们可以要求所有的客户端需要在请求头中携带来源信息
         */</span>
        <span class="token class-name">String</span> serviceName <span class="token operator">=</span> request<span class="token punctuation">.</span><span class="token function">getParameter</span><span class="token punctuation">(</span><span class="token string">&quot;serviceName&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> serviceName<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div></li> <li><p><strong>在shop-order-server中新建AuthController.java,代码如下:</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@RestController</span>
<span class="token annotation punctuation">@Slf4j</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">AuthController</span> <span class="token punctuation">{</span>
    <span class="token annotation punctuation">@RequestMapping</span><span class="token punctuation">(</span><span class="token string">&quot;/auth1&quot;</span><span class="token punctuation">)</span>
    <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">auth1</span><span class="token punctuation">(</span><span class="token class-name">String</span> serviceName<span class="token punctuation">)</span><span class="token punctuation">{</span>
        log<span class="token punctuation">.</span><span class="token function">info</span><span class="token punctuation">(</span><span class="token string">&quot;应用:{},访问接口&quot;</span><span class="token punctuation">,</span>serviceName<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token string">&quot;auth1&quot;</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div></li> <li><p><strong>新增授权规则</strong></p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20201030110821985.png" alt="image-20201030110821985"></strong></p></li> <li><p><strong>访问测试</strong></p> <p>访问http://localhost:8091/auth1?serviceName=pc 不能访问</p> <p>访问http://localhost:8091/auth1?serviceName=app 可以访问</p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20201030110934370.png" alt="image-20201030110934370"></strong></p></li></ol> <h3 id="_7-9-sentinel规则-系统规则"><a href="#_7-9-sentinel规则-系统规则" class="header-anchor">#</a> 7.9 Sentinel规则-系统规则</h3> <p>系统保护规则是从应用级别的入口流量进行控制，从单台机器的 load、CPU 使用率、平均 RT、入口 QPS 和并发线程数等几个维度监控应用指标，让系统尽可能跑在最大吞吐量的同时保证系统整体的稳定性。</p> <p>系统保护规则是应用整体维度的，而不是资源维度的，并且<strong>仅对入口流量生效</strong>。入口流量指的是进入应用的流量（<code>EntryType.IN</code>），比如 Web 服务或 Dubbo 服务端接收的请求，都属于入口流量。</p> <p>系统规则支持以下的模式：</p> <ul><li><strong>Load 自适应</strong>（仅对 Linux/Unix-like 机器生效）：系统的 load1 作为启发指标，进行自适应系统保护。当系统 load1 超过设定的启发值，且系统当前的并发线程数超过估算的系统容量时才会触发系统保护（BBR 阶段）。系统容量由系统的 <code>maxQps * minRt</code> 估算得出。设定参考值一般是 <code>CPU cores * 2.5</code>。</li> <li><strong>CPU usage</strong>（1.5.0+ 版本）：当系统 CPU 使用率超过阈值即触发系统保护（取值范围 0.0-1.0），比较灵敏。</li> <li><strong>平均 RT</strong>：当单台机器上所有入口流量的平均 RT 达到阈值即触发系统保护，单位是毫秒。</li> <li><strong>并发线程数</strong>：当单台机器上所有入口流量的并发线程数达到阈值即触发系统保护。</li> <li><strong>入口 QPS</strong>：当单台机器上所有入口流量的 QPS 达到阈值即触发系统保护。</li></ul> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20201030111407594.png" alt="image-20201030111407594"></strong></p> <h3 id="_7-10-sentinel-自定义异常返回"><a href="#_7-10-sentinel-自定义异常返回" class="header-anchor">#</a> 7.10  Sentinel 自定义异常返回</h3> <p>当前面设定的规则没有满足是，我们可以自定义异常返回.</p> <ul><li><p>FlowException 限流异常</p></li> <li><p>DegradeException 降级异常</p></li> <li><p>ParamFlowException 参数限流异常</p></li> <li><p>AuthorityException 授权异常</p></li> <li><p>SystemBlockException 系统负载异常</p></li></ul> <p>在shop-order-server项目中定义异常返回处理类</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">package</span> <span class="token namespace">cn<span class="token punctuation">.</span>wolfcode<span class="token punctuation">.</span>error</span><span class="token punctuation">;</span>
<span class="token annotation punctuation">@Component</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">ExceptionHandlerPage</span> <span class="token keyword">implements</span> <span class="token class-name">BlockExceptionHandler</span> <span class="token punctuation">{</span>
    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">handle</span><span class="token punctuation">(</span><span class="token class-name">HttpServletRequest</span> request<span class="token punctuation">,</span> <span class="token class-name">HttpServletResponse</span> response<span class="token punctuation">,</span> <span class="token class-name">BlockException</span> e<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">Exception</span> <span class="token punctuation">{</span>
        response<span class="token punctuation">.</span><span class="token function">setContentType</span><span class="token punctuation">(</span><span class="token string">&quot;application/json;charset=utf-8&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">ResultData</span> data <span class="token operator">=</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>e <span class="token keyword">instanceof</span> <span class="token class-name">FlowException</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            data <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ResultData</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token string">&quot;接口被限流了&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>e <span class="token keyword">instanceof</span> <span class="token class-name">DegradeException</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            data <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ResultData</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token string">&quot;接口被降级了&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span><span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>e <span class="token keyword">instanceof</span> <span class="token class-name">ParamFlowException</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            data <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ResultData</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">3</span><span class="token punctuation">,</span> <span class="token string">&quot;参数限流异常&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span><span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>e <span class="token keyword">instanceof</span> <span class="token class-name">AuthorityException</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            data <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ResultData</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token string">&quot;授权异常&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span><span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>e <span class="token keyword">instanceof</span> <span class="token class-name">SystemBlockException</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            data <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ResultData</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">5</span><span class="token punctuation">,</span> <span class="token string">&quot;系统负载异常了...&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        response<span class="token punctuation">.</span><span class="token function">getWriter</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">write</span><span class="token punctuation">(</span><span class="token constant">JSON</span><span class="token punctuation">.</span><span class="token function">toJSONString</span><span class="token punctuation">(</span>data<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token annotation punctuation">@Data</span>
<span class="token annotation punctuation">@AllArgsConstructor</span><span class="token comment">//全参构造</span>
<span class="token annotation punctuation">@NoArgsConstructor</span><span class="token comment">//无参构造</span>
<span class="token keyword">class</span> <span class="token class-name">ResultData</span> <span class="token punctuation">{</span>
    <span class="token keyword">private</span> <span class="token keyword">int</span> code<span class="token punctuation">;</span>
    <span class="token keyword">private</span> <span class="token class-name">String</span> message<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br></div></div><h3 id="_7-11-sentinelresource的使用"><a href="#_7-11-sentinelresource的使用" class="header-anchor">#</a> 7.11 @SentinelResource的使用</h3> <p>在定义了资源点之后，我们可以通过Dashboard来设置限流和降级策略来对资源点进行保护。同时还能</p> <p>通过@SentinelResource来指定出现异常时的处理策略。</p> <p>@SentinelResource 用于定义资源，并提供可选的异常处理和 fallback 配置项。</p> <p>其主要参数如下:</p> <table><thead><tr><th style="text-align:left;">属性</th> <th>作用</th></tr></thead> <tbody><tr><td style="text-align:left;">value</td> <td>资源名称，必需项（不能为空）</td></tr> <tr><td style="text-align:left;">entryType</td> <td>entry 类型，可选项（默认为 <code>EntryType.OUT</code>）</td></tr> <tr><td style="text-align:left;">blockHandler<code>/</code>blockHandlerClass</td> <td><code>blockHandler</code> 对应处理 <code>BlockException</code> 的函数名称，可选项。blockHandler 函数访问范围需要是 <code>public</code>，返回类型需要与原方法相匹配，参数类型需要和原方法相匹配并且最后加一个额外的参数，类型为 <code>BlockException</code>。blockHandler 函数默认需要和原方法在同一个类中。若希望使用其他类的函数，则可以指定 <code>blockHandlerClass</code> 为对应的类的 <code>Class</code> 对象，注意对应的函数必需为 static 函数，否则无法解析。</td></tr> <tr><td style="text-align:left;">fallback<code>/</code>fallbackClass</td> <td>fallback 函数名称，可选项，用于在抛出异常的时候提供 fallback 处理逻辑。fallback 函数可以针对所有类型的异常（除了 <code>exceptionsToIgnore</code> 里面排除掉的异常类型）进行处理。fallback 函数签名和位置要求：<br>1. 返回值类型必须与原函数返回值类型一致； <br>2.方法参数列表需要和原函数一致，或者可以额外多一个 <code>Throwable</code> 类型的参数用于接收对应的异常。<br>3.fallback 函数默认需要和原方法在同一个类中。若希望使用其他类的函数，则可以指定 <code>fallbackClass</code> 为对应的类的 <code>Class</code> 对象，注意对应的函数必需为 static 函数，否则无法解析。</td></tr> <tr><td style="text-align:left;"><code>defaultFallback</code></td> <td>默认的 fallback 函数名称，可选项，通常用于通用的 fallback 逻辑（即可以用于很多服务或方法）。默认 fallback 函数可以针对所有类型的异常（除了 <code>exceptionsToIgnore</code> 里面排除掉的异常类型）进行处理。若同时配置了 fallback 和 defaultFallback，则只有 fallback 会生效。defaultFallback 函数签名要求：<br>1. 返回值类型必须与原函数返回值类型一致；<br>2. 方法参数列表需要为空，或者可以额外多一个 <code>Throwable</code> 类型的参数用于接收对应的异常。 <br>3. defaultFallback 函数默认需要和原方法在同一个类中。若希望使用其他类的函数，则可以指定 <code>fallbackClass</code> 为对应的类的 <code>Class</code> 对象，注意对应的函数必需为 static 函数，否则无法解析。</td></tr> <tr><td style="text-align:left;"><code>exceptionsToIgnore</code></td> <td>用于指定哪些异常被排除掉，不会计入异常统计中，也不会进入 fallback 逻辑中，而是会原样抛出。</td></tr></tbody></table> <p><strong>定义限流和降级后的处理方法</strong></p> <p>直接将限流和降级方法定义在方法中</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">package</span> <span class="token namespace">cn<span class="token punctuation">.</span>wolfcode<span class="token punctuation">.</span>controller</span><span class="token punctuation">;</span>
<span class="token annotation punctuation">@RestController</span>
<span class="token annotation punctuation">@Slf4j</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">AnnoController</span> <span class="token punctuation">{</span>
    <span class="token annotation punctuation">@RequestMapping</span><span class="token punctuation">(</span><span class="token string">&quot;/anno1&quot;</span><span class="token punctuation">)</span>
    <span class="token annotation punctuation">@SentinelResource</span><span class="token punctuation">(</span>value <span class="token operator">=</span> <span class="token string">&quot;anno1&quot;</span><span class="token punctuation">,</span>
            blockHandler<span class="token operator">=</span><span class="token string">&quot;anno1BlockHandler&quot;</span><span class="token punctuation">,</span>
            fallback <span class="token operator">=</span> <span class="token string">&quot;anno1Fallback&quot;</span>
    <span class="token punctuation">)</span>
    <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">anno1</span><span class="token punctuation">(</span><span class="token class-name">String</span> name<span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token string">&quot;wolfcode&quot;</span><span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span>name<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
            <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">RuntimeException</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token keyword">return</span> <span class="token string">&quot;anno1&quot;</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">anno1BlockHandler</span><span class="token punctuation">(</span><span class="token class-name">String</span> name<span class="token punctuation">,</span><span class="token class-name">BlockException</span> ex<span class="token punctuation">)</span><span class="token punctuation">{</span>
        log<span class="token punctuation">.</span><span class="token function">error</span><span class="token punctuation">(</span><span class="token string">&quot;{}&quot;</span><span class="token punctuation">,</span> ex<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token string">&quot;接口被限流或者降级了&quot;</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token comment">//Throwable时进入的方法</span>
    <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">anno1Fallback</span><span class="token punctuation">(</span><span class="token class-name">String</span> name<span class="token punctuation">,</span><span class="token class-name">Throwable</span> throwable<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        log<span class="token punctuation">.</span><span class="token function">error</span><span class="token punctuation">(</span><span class="token string">&quot;{}&quot;</span><span class="token punctuation">,</span> throwable<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token string">&quot;接口发生异常了&quot;</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br></div></div><h3 id="_7-12-feign整合sentinel"><a href="#_7-12-feign整合sentinel" class="header-anchor">#</a> 7.12 Feign整合Sentinel</h3> <ol><li><p><strong>在shop-order-server项目的配置文件中开启feign对Sentinel的支持</strong></p> <div class="language-yaml line-numbers-mode"><pre class="language-yaml"><code><span class="token key atrule">feign</span><span class="token punctuation">:</span>
  <span class="token key atrule">sentinel</span><span class="token punctuation">:</span>
    <span class="token key atrule">enabled</span><span class="token punctuation">:</span> <span class="token boolean important">true</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div></li> <li><p><strong>创建容错类</strong></p></li></ol> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@Component</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">ProductFeignFallBack</span> <span class="token keyword">implements</span> <span class="token class-name">IProductFeginService</span> <span class="token punctuation">{</span>
    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">public</span> <span class="token class-name">Product</span> <span class="token function">findByPid</span><span class="token punctuation">(</span><span class="token class-name">Long</span> pid<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">Product</span> product <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Product</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        product<span class="token punctuation">.</span><span class="token function">setPid</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1L</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        product<span class="token punctuation">.</span><span class="token function">setPname</span><span class="token punctuation">(</span><span class="token string">&quot;兜底数据&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        product<span class="token punctuation">.</span><span class="token function">setPprice</span><span class="token punctuation">(</span><span class="token number">0.0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> product<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><ol start="3"><li><p><strong>在feign接口中定义容错类</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@FeignClient</span><span class="token punctuation">(</span>name <span class="token operator">=</span> <span class="token string">&quot;product-service&quot;</span><span class="token punctuation">,</span>fallback <span class="token operator">=</span> <span class="token class-name">ProductFeignFallBack</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span>
<span class="token keyword">public</span> <span class="token keyword">interface</span> <span class="token class-name">ProductFeignApi</span> <span class="token punctuation">{</span>
    <span class="token annotation punctuation">@RequestMapping</span><span class="token punctuation">(</span><span class="token string">&quot;/product/{pid}&quot;</span><span class="token punctuation">)</span>
    <span class="token keyword">public</span> <span class="token class-name">Product</span> <span class="token function">findByPid</span><span class="token punctuation">(</span><span class="token annotation punctuation">@PathVariable</span><span class="token punctuation">(</span><span class="token string">&quot;pid&quot;</span><span class="token punctuation">)</span> <span class="token class-name">Long</span> pid<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div></li> <li><p><strong>停止所有 商品服务,重启 shop-order 服务,访问请求,观察容错效果</strong></p></li></ol> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20201030141556327.png" alt="image-20201030141556327"></strong></p> <p>可能上面的案例并不是特别恰当，我们只是通过案例来演示Feign集成Sentinel实现降级的效果. 接下来我们具体更贴切的案例来讲解Feign降级的作用.</p> <p>比如我们在购物的时候，查看商品详情页面的时候，里面包含库存信息,商品详情信息,评论信息，这个需求包含的微服务如下:</p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20201030142332530.png" alt="image-20201030142332530"></strong></p> <p>假设现在评论服务宕机了,那是不是意味用户发出查看商品请求也无法正常显示了，商品都看不到了，那用户也无法进行下单的操作了. 但是对于用户来说，评论看不到并不影响他购物，所以这时候我们应该对评论服务进行及·降级处理，返回一个兜底数据(空数据)，这样用户的查看商品请求能正常显示，只是评论数据看不到而已，这样的话，用户的下单请求也不会受到影响.</p> <h2 id="八、服务网关gateway"><a href="#八、服务网关gateway" class="header-anchor">#</a> 八、服务网关Gateway</h2> <p>大家都都知道在微服务架构中，一个系统会被拆分为很多个微服务。那么作为客户端要如何去调用</p> <p>这么多的微服务呢？如果没有网关的存在，我们只能在客户端记录每个微服务的地址，然后分别去调</p> <p>用。</p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20201030144013742.png" alt="image-20201030144013742"></strong></p> <p>这样的架构，会存在着诸多的问题：</p> <ul><li><p>客户端多次请求不同的微服务，增加客户端代码或配置编写的复杂性</p></li> <li><p>认证复杂，每个服务都需要独立认证。</p></li> <li><p>微服务做集群的情况下，客户端并没有负责均衡的功能</p></li></ul> <p>上面的这些问题可以借助<strong>API网关</strong>来解决。</p> <p>所谓的API网关，就是指系统的<strong>统一入口</strong>，它封装了应用程序的内部结构，为客户端提供统一服</p> <p>务，一些与业务本身功能无关的公共逻辑可以在这里实现，诸如认证、鉴权、监控、路由转发等等。</p> <p>添加上API网关之后，系统的架构图变成了如下所示：</p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20201030144534269.png" alt="image-20201030144534269"></strong></p> <p>网关是如何知道微服务的地址?网关如何进行负载均衡呢？</p> <p>网关需要将自己的信息注册到注册中心上并且拉取其他微服务的信息，然后再调用的时候基于Ribbon实现负载均衡。</p> <p>网关的作用：</p> <p><strong>1&gt;请求分发  2&gt;负载均衡  3&gt;过滤拦截   4&gt;网络隔离</strong></p> <h3 id="_8-1-常见网关介绍"><a href="#_8-1-常见网关介绍" class="header-anchor">#</a> 8.1 常见网关介绍</h3> <ul><li><strong>Nginx+lua</strong></li></ul> <p>使用nginx的反向代理和负载均衡可实现对api服务器的负载均衡及高可用,lua是一种脚本语言,可以来编写一些简单的逻辑, nginx支持lua脚本</p> <ul><li><strong>Kong</strong></li></ul> <p>基于Nginx+Lua开发，性能高，稳定，有多个可用的插件(限流、鉴权等等)可以开箱即用。 问题：只支持Http协议；二次开发，自由扩展困难；提供管理API，缺乏更易用的管控、配置方式。</p> <ul><li><strong>Zuul</strong></li></ul> <p>Netflix开源的网关，功能丰富，使用JAVA开发，易于二次开发 问题：缺乏管控，无法动态配置；依赖组件较多；处理Http请求依赖的是Web容器，性能不如Nginx,Spring Cloud Gateway</p> <ul><li><strong>Gateway</strong></li></ul> <p>Spring公司为了替换Zuul而开发的网关服务</p> <p>注意：SpringCloud alibaba技术栈中并没有提供自己的网关，我们可以采用Spring Cloud Gateway来做网关</p> <h3 id="_8-2-gateway简介"><a href="#_8-2-gateway简介" class="header-anchor">#</a> 8.2 Gateway简介</h3> <p>​	Spring Cloud Gateway是Spring公司基于Spring 5.0，Spring Boot 2.0 和 Project Reactor 等技术开发的网关，它旨在为微服务架构提供一种简单有效的统一的 API 路由管理方式。它的目标是替代Netflix Zuul，其不仅提供统一的路由方式，并且基于 Filter 链的方式提供了网关基本的功能，例如：安全，监控和限流。</p> <p><strong>优点：</strong></p> <ul><li><p>性能强劲：是第一代网关Zuul的1.6倍</p></li> <li><p>功能强大：内置了很多实用的功能，例如转发、监控、限流等</p></li> <li><p>设计优雅，容易扩展</p></li></ul> <p><strong>缺点：</strong></p> <ul><li><p>其实现依赖Netty与WebFlux，不是传统的Servlet编程模型，学习成本高</p></li> <li><p>不能将其部署在Tomcat、Jetty等Servlet容器里，只能打成jar包执行</p></li> <li><p>需要Spring Boot 2.0及以上的版本，才支持</p></li></ul> <h3 id="_8-3-gateway快速入门"><a href="#_8-3-gateway快速入门" class="header-anchor">#</a> 8.3 Gateway快速入门</h3> <ol><li><p><strong>创建一个 shop-gateway 的模块,导入相关依赖</strong></p> <div class="language-xml line-numbers-mode"><pre class="language-xml"><code><span class="token prolog">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>project</span> <span class="token attr-name">xmlns</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>http://maven.apache.org/POM/4.0.0<span class="token punctuation">&quot;</span></span>
         <span class="token attr-name"><span class="token namespace">xmlns:</span>xsi</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>http://www.w3.org/2001/XMLSchema-instance<span class="token punctuation">&quot;</span></span>
         <span class="token attr-name"><span class="token namespace">xsi:</span>schemaLocation</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>parent</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>shop-parent<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>cn.wolfcode<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">&gt;</span></span>1.0.0<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>parent</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>modelVersion</span><span class="token punctuation">&gt;</span></span>4.0.0<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>modelVersion</span><span class="token punctuation">&gt;</span></span>

    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>api-gateway<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependencies</span><span class="token punctuation">&gt;</span></span>
        <span class="token comment">&lt;!--gateway网关--&gt;</span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">&gt;</span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>org.springframework.cloud<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>spring-cloud-starter-gateway<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">&gt;</span></span>
        <span class="token comment">&lt;!--nacos客户端--&gt;</span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">&gt;</span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>com.alibaba.cloud<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>spring-cloud-starter-alibaba-nacos-discovery<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">&gt;</span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>org.projectlombok<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>lombok<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependencies</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>project</span><span class="token punctuation">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br></div></div></li> <li><p><strong>编写启动类</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@SpringBootApplication</span>
<span class="token annotation punctuation">@EnableDiscoveryClient</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">ApiGatewayServer</span> <span class="token punctuation">{</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">SpringApplication</span><span class="token punctuation">.</span><span class="token function">run</span><span class="token punctuation">(</span><span class="token class-name">ApiGatewayServer</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">,</span>args<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div></li> <li><p><strong>编写配置文件</strong></p> <div class="language-yaml line-numbers-mode"><pre class="language-yaml"><code><span class="token key atrule">server</span><span class="token punctuation">:</span>
  <span class="token key atrule">port</span><span class="token punctuation">:</span> <span class="token number">9000</span>
<span class="token key atrule">spring</span><span class="token punctuation">:</span>
  <span class="token key atrule">application</span><span class="token punctuation">:</span>
    <span class="token key atrule">name</span><span class="token punctuation">:</span> shop<span class="token punctuation">-</span>gateway
  <span class="token key atrule">cloud</span><span class="token punctuation">:</span>
    <span class="token key atrule">nacos</span><span class="token punctuation">:</span>
      <span class="token key atrule">discovery</span><span class="token punctuation">:</span>
        <span class="token key atrule">server-addr</span><span class="token punctuation">:</span> localhost<span class="token punctuation">:</span><span class="token number">8848</span>
    <span class="token key atrule">gateway</span><span class="token punctuation">:</span>
      <span class="token key atrule">discovery</span><span class="token punctuation">:</span>
        <span class="token key atrule">locator</span><span class="token punctuation">:</span>
          <span class="token key atrule">enabled</span><span class="token punctuation">:</span> <span class="token boolean important">true</span> <span class="token comment">## 让gateway可以发现nacos中的微服务</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br></div></div></li> <li><p><strong>启动测试</strong></p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20201030152917728.png" alt="image-20201030152917728"></strong></p></li></ol> <h3 id="_8-4-自定义路由规则"><a href="#_8-4-自定义路由规则" class="header-anchor">#</a> 8.4 自定义路由规则</h3> <ol><li><p><strong>在application.yml中添加新的路由规则</strong></p> <div class="language-yaml line-numbers-mode"><pre class="language-yaml"><code><span class="token key atrule">spring</span><span class="token punctuation">:</span>
  <span class="token key atrule">cloud</span><span class="token punctuation">:</span>
    <span class="token key atrule">gateway</span><span class="token punctuation">:</span>
      <span class="token key atrule">routes</span><span class="token punctuation">:</span>
        <span class="token punctuation">-</span> <span class="token key atrule">id</span><span class="token punctuation">:</span> product_route
          <span class="token key atrule">uri</span><span class="token punctuation">:</span> lb<span class="token punctuation">:</span>//product<span class="token punctuation">-</span>service 
          <span class="token key atrule">predicates</span><span class="token punctuation">:</span>
            <span class="token punctuation">-</span> Path=/product<span class="token punctuation">-</span>serv/<span class="token important">**</span>
          <span class="token key atrule">filters</span><span class="token punctuation">:</span>
            <span class="token punctuation">-</span> StripPrefix=1
        <span class="token punctuation">-</span> <span class="token key atrule">id</span><span class="token punctuation">:</span> order_route
          <span class="token key atrule">uri</span><span class="token punctuation">:</span> lb<span class="token punctuation">:</span>//order<span class="token punctuation">-</span>service 
          <span class="token key atrule">predicates</span><span class="token punctuation">:</span>
            <span class="token punctuation">-</span> Path=/order<span class="token punctuation">-</span>serv/<span class="token important">**</span>
          <span class="token key atrule">filters</span><span class="token punctuation">:</span>
            <span class="token punctuation">-</span> StripPrefix=1
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br></div></div></li> <li><p><strong>启动测试</strong></p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20201030154027833.png" alt="image-20201030154027833"></strong></p></li></ol> <h3 id="_8-5-gateway概念"><a href="#_8-5-gateway概念" class="header-anchor">#</a> 8.5 Gateway概念</h3> <p>路由(Route) 是 gateway 中最基本的组件之一，表示一个具体的路由信息载体。主要定义了下面的几个</p> <p>信息:</p> <ul><li><p><strong>id</strong>，路由标识符，区别于其他 Route。</p></li> <li><p><strong>uri</strong>，路由指向的目的地 uri，即客户端请求最终被转发到的微服务。</p></li> <li><p><strong>order</strong>，用于多个 Route 之间的排序，数值越小排序越靠前，匹配优先级越高。</p></li> <li><p><strong>predicate</strong>，断言的作用是进行条件判断，只有断言都返回真，才会真正的执行路由。</p></li> <li><p><strong>filter</strong>，过滤器用于修改请求和响应信息。</p></li></ul> <p>执行流程图:</p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20201030161652819.png" alt="image-20201030161652819"></strong></p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/20210608172348987.jpg" alt="20210608172348987"></p> <h3 id="_8-6-过滤器filter"><a href="#_8-6-过滤器filter" class="header-anchor">#</a> 8.6 过滤器Filter</h3> <p>过滤器就是在请求的传递过程中,对请求和响应做一些手脚.</p> <p>在Gateway中, Filter的生命周期只有两个：“pre” 和 “post”。</p> <ul><li>PRE： 这种过滤器在请求被路由之前调用。我们可利用这种过滤器实现身份验证、在集群中选择</li></ul> <p>请求的微服务、记录调试信息等。</p> <ul><li>POST：这种过滤器在路由到微服务以后执行。这种过滤器可用来为响应添加标准的HTTP</li></ul> <p>Header、收集统计信息和指标、将响应从微服务发送给客户端等。</p> <p>在Gateway中，Filter的作用范围两种:</p> <ul><li>GatewayFilter：应用到单个路由或者一个分组的路由上。</li> <li>GlobalFilter：应用到所有的路由上</li></ul> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/20210608172400656.jpg" alt="在这里插入图片描述"></p> <h4 id="_8-6-1-局部路由过滤器"><a href="#_8-6-1-局部路由过滤器" class="header-anchor">#</a> 8.6.1 局部路由过滤器</h4> <p>局部过滤器是针对单个路由的过滤器,在SpringCloud Gateway中内置了很多不同类型的网关路由过滤器,有兴趣同学可以自行了解，这里太多了，我们就不一一讲解，我们主要来讲一下自定义路由过滤器。</p> <p>需求: 统计订单服务调用耗时.</p> <p>流程图</p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20221017165145458.png" alt="image-20221017165145458"></p> <p><strong>1&gt;编写Filter类，需要实现GatewayFilter接口，重写filter方法</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">package</span> <span class="token namespace">cn<span class="token punctuation">.</span>wolfcode<span class="token punctuation">.</span>filters</span><span class="token punctuation">;</span>

<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>cloud<span class="token punctuation">.</span>gateway<span class="token punctuation">.</span>filter<span class="token punctuation">.</span></span><span class="token class-name">GatewayFilter</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>cloud<span class="token punctuation">.</span>gateway<span class="token punctuation">.</span>filter<span class="token punctuation">.</span></span><span class="token class-name">GatewayFilterChain</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>web<span class="token punctuation">.</span>server<span class="token punctuation">.</span></span><span class="token class-name">ServerWebExchange</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">reactor<span class="token punctuation">.</span>core<span class="token punctuation">.</span>publisher<span class="token punctuation">.</span></span><span class="token class-name">Mono</span></span><span class="token punctuation">;</span>

<span class="token comment">/**
 * 计算时间过滤器
 */</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">TimeGatewayFilter</span> <span class="token keyword">implements</span> <span class="token class-name">GatewayFilter</span><span class="token punctuation">,</span> <span class="token class-name">Ordered</span>  <span class="token punctuation">{</span>
    <span class="token comment">/**
     * 过滤逻辑
     * @param exchange 转换器--封装了来自请求中所有信息，比如：请求方法，请求参数，请求路径，请求头，cookie等
     * @param chain 过滤器链--使用责任链模式，决定当前过滤器是放行还是拒绝
     * @return Mono 响应式编程的返回值规范，一般后置拦截才会用
     */</span>
    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">public</span> <span class="token class-name">Mono</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Void</span><span class="token punctuation">&gt;</span></span> <span class="token function">filter</span><span class="token punctuation">(</span><span class="token class-name">ServerWebExchange</span> exchange<span class="token punctuation">,</span> <span class="token class-name">GatewayFilterChain</span> chain<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">//统计时间：进来(pre前置)记当前时间， 出去(post后置)记当前时间，2者差值就是运行时间</span>
        <span class="token keyword">long</span> begin <span class="token operator">=</span> <span class="token class-name">System</span><span class="token punctuation">.</span><span class="token function">currentTimeMillis</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;前置当前时间：&quot;</span> <span class="token operator">+</span> begin<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> chain<span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">(</span>exchange<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span><span class="token class-name">Mono</span><span class="token punctuation">.</span><span class="token function">fromRunnable</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-&gt;</span><span class="token punctuation">{</span>
            <span class="token keyword">long</span> end <span class="token operator">=</span> <span class="token class-name">System</span><span class="token punctuation">.</span><span class="token function">currentTimeMillis</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;后置当前时间：&quot;</span> <span class="token operator">+</span> end<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;两者时间差：&quot;</span> <span class="token operator">+</span> <span class="token punctuation">(</span>end<span class="token operator">-</span>begin<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">public</span> <span class="token keyword">int</span> <span class="token function">getOrder</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br></div></div><p><strong>2&gt;往Gateway中注册自定义的filter类</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token comment">/**
 * 注册时间统计过滤器
 * 要求：
 * 1&gt;过滤器工厂名字必须按照规定格式，否则报错。
 *   格式： XxxGatewayFilterFactory， 其中Xxx 就是在yml中filters配置过滤器
 * 2&gt;继承AbstractGatewayFilterFactory父类，明确指定一个泛型
 *   改泛型用于接受yml-filters配置的过滤器值
 */</span>
<span class="token annotation punctuation">@Component</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">TimeGatewayFilterFactory</span> <span class="token keyword">extends</span> <span class="token class-name">AbstractGatewayFilterFactory</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Object</span><span class="token punctuation">&gt;</span></span> <span class="token punctuation">{</span>
    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">public</span> <span class="token class-name">GatewayFilter</span> <span class="token function">apply</span><span class="token punctuation">(</span><span class="token class-name">Object</span> config<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">TimeGatewayFilter</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br></div></div><p><strong>3&gt;配置使用自定义filter类</strong></p> <div class="language-yaml line-numbers-mode"><pre class="language-yaml"><code><span class="token key atrule">server</span><span class="token punctuation">:</span>
  <span class="token key atrule">port</span><span class="token punctuation">:</span> <span class="token number">9000</span>
<span class="token key atrule">spring</span><span class="token punctuation">:</span>
  <span class="token key atrule">application</span><span class="token punctuation">:</span>
    <span class="token key atrule">name</span><span class="token punctuation">:</span> shop<span class="token punctuation">-</span>gateway
  <span class="token key atrule">cloud</span><span class="token punctuation">:</span>
    <span class="token key atrule">nacos</span><span class="token punctuation">:</span>
      <span class="token key atrule">discovery</span><span class="token punctuation">:</span>
        <span class="token key atrule">server-addr</span><span class="token punctuation">:</span> localhost<span class="token punctuation">:</span><span class="token number">8848</span>
    <span class="token key atrule">gateway</span><span class="token punctuation">:</span>
      <span class="token key atrule">discovery</span><span class="token punctuation">:</span>
        <span class="token key atrule">locator</span><span class="token punctuation">:</span>
          <span class="token key atrule">enabled</span><span class="token punctuation">:</span> <span class="token boolean important">true</span> 
      <span class="token key atrule">routes</span><span class="token punctuation">:</span>
        <span class="token punctuation">-</span> <span class="token key atrule">id</span><span class="token punctuation">:</span> product_route
          <span class="token key atrule">uri</span><span class="token punctuation">:</span> lb<span class="token punctuation">:</span>//product<span class="token punctuation">-</span>service 
          <span class="token key atrule">predicates</span><span class="token punctuation">:</span>
            <span class="token punctuation">-</span> Path=/product<span class="token punctuation">-</span>serv/<span class="token important">**</span>
          <span class="token key atrule">filters</span><span class="token punctuation">:</span>
            <span class="token punctuation">-</span> StripPrefix=1
        <span class="token punctuation">-</span> <span class="token key atrule">id</span><span class="token punctuation">:</span> order_route
          <span class="token key atrule">uri</span><span class="token punctuation">:</span> lb<span class="token punctuation">:</span>//order<span class="token punctuation">-</span>service 
          <span class="token key atrule">predicates</span><span class="token punctuation">:</span>
            <span class="token punctuation">-</span> Path=/order<span class="token punctuation">-</span>serv/<span class="token important">**</span>
          <span class="token key atrule">filters</span><span class="token punctuation">:</span>
            <span class="token punctuation">-</span> StripPrefix=1
            <span class="token punctuation">-</span> Time
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br></div></div><p><strong>4&gt;测试，访问订单服务save方法</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code>前置当前时间：<span class="token number">1666065937548</span>
后置当前时间：<span class="token number">1666065943803</span>
两者时间差：<span class="token number">6255</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p><strong>拓展：如果想弄带参数过滤器，比如</strong>：</p> <div class="language-yaml line-numbers-mode"><pre class="language-yaml"><code><span class="token key atrule">filters</span><span class="token punctuation">:</span>
    <span class="token punctuation">-</span> StripPrefix=1
    <span class="token punctuation">-</span> Time=111<span class="token punctuation">,</span><span class="token number">222</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p><strong>1&gt;定制接受参数的对象</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@Getter</span>
<span class="token annotation punctuation">@Setter</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">TimeGatewayFilterParam</span> <span class="token punctuation">{</span>
    <span class="token keyword">private</span> <span class="token class-name">String</span> value1<span class="token punctuation">;</span>
    <span class="token keyword">private</span> <span class="token class-name">String</span> value2<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p><strong>2&gt;修改TimeGatewayFilter 过滤器类，获取Gateway传入的参数对象</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token comment">/**
 * 计算时间过滤器
 */</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">TimeGatewayFilter</span> <span class="token keyword">implements</span> <span class="token class-name">GatewayFilter</span> <span class="token punctuation">,</span> <span class="token class-name">Ordered</span>  <span class="token punctuation">{</span>

    <span class="token keyword">private</span> <span class="token class-name">TimeGatewayFilterParam</span> value<span class="token punctuation">;</span>
    <span class="token keyword">public</span> <span class="token class-name">TimeGatewayFilter</span><span class="token punctuation">(</span><span class="token class-name">TimeGatewayFilterParam</span> config<span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>value <span class="token operator">=</span> config<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token comment">/**
     * 过滤逻辑
     * @param exchange 转换器--封装了来自请求中所有信息，比如：请求方法，请求参数，请求路径，请求头，cookie等
     * @param chain 过滤器链--使用责任链模式，决定当前过滤器是放行还是拒绝
     * @return Mono 响应式编程的返回值规范，一般后置拦截才会用
     */</span>
    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">public</span> <span class="token class-name">Mono</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Void</span><span class="token punctuation">&gt;</span></span> <span class="token function">filter</span><span class="token punctuation">(</span><span class="token class-name">ServerWebExchange</span> exchange<span class="token punctuation">,</span> <span class="token class-name">GatewayFilterChain</span> chain<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">//统计时间：进来(前置)记当前时间， 出去(后置)记当前时间，2者差值就是运行时间</span>
        <span class="token keyword">long</span> begin <span class="token operator">=</span> <span class="token class-name">System</span><span class="token punctuation">.</span><span class="token function">currentTimeMillis</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>value<span class="token punctuation">.</span><span class="token function">getValue1</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">&quot;前置当前时间：&quot;</span> <span class="token operator">+</span> begin<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> chain<span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">(</span>exchange<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span><span class="token class-name">Mono</span><span class="token punctuation">.</span><span class="token function">fromRunnable</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-&gt;</span><span class="token punctuation">{</span>
            <span class="token keyword">long</span> end <span class="token operator">=</span> <span class="token class-name">System</span><span class="token punctuation">.</span><span class="token function">currentTimeMillis</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>value<span class="token punctuation">.</span><span class="token function">getValue2</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">&quot;后置当前时间：&quot;</span> <span class="token operator">+</span> end<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;两者时间差：&quot;</span> <span class="token operator">+</span> <span class="token punctuation">(</span>end<span class="token operator">-</span>begin<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">public</span> <span class="token keyword">int</span> <span class="token function">getOrder</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br></div></div><p><strong>3&gt;在TimeGatewayFilterFactory 类中编写Gateway传入的参数对象逻辑</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@Component</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">TimeGatewayFilterFactory</span> <span class="token keyword">extends</span> <span class="token class-name">AbstractGatewayFilterFactory</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">TimeGatewayFilterParam</span><span class="token punctuation">&gt;</span></span> <span class="token punctuation">{</span>

    <span class="token comment">//指定获取配置参数之后封装成啥对象</span>
    <span class="token keyword">public</span> <span class="token class-name">TimeGatewayFilterFactory</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token keyword">super</span><span class="token punctuation">(</span><span class="token class-name">TimeGatewayFilterParam</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token comment">//将数据添加到哪个属性上</span>
    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">public</span> <span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">&gt;</span></span> <span class="token function">shortcutFieldOrder</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token class-name">Arrays</span><span class="token punctuation">.</span><span class="token function">asList</span><span class="token punctuation">(</span><span class="token string">&quot;value1&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;value2&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">public</span> <span class="token class-name">GatewayFilter</span> <span class="token function">apply</span><span class="token punctuation">(</span><span class="token class-name">TimeGatewayFilterParam</span> config<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">TimeGatewayFilter</span><span class="token punctuation">(</span>config<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br></div></div><h4 id="_8-6-2-全局路由过滤器"><a href="#_8-6-2-全局路由过滤器" class="header-anchor">#</a> 8.6.2 全局路由过滤器</h4> <p>全局过滤器作用于所有路由, 无需配置。通过全局过滤器可以实现对权限的统一校验，安全性验证等功</p> <p>能。</p> <p>SpringCloud Gateway内部也是通过一系列的内置全局过滤器对整个路由转发进行处理如下：</p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/gatewayfilter.jpg" alt=""></strong></p> <p>需求: 实现统一鉴权的功能,我们需要在网关判断请求中是否包含token且，如果没有则不转发路由，有则执行正常逻辑。</p> <p><strong>1&gt;编写全局过滤类</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">package</span> <span class="token namespace">cn<span class="token punctuation">.</span>wolfcode<span class="token punctuation">.</span>filters</span><span class="token punctuation">;</span>

<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>cloud<span class="token punctuation">.</span>gateway<span class="token punctuation">.</span>filter<span class="token punctuation">.</span></span><span class="token class-name">GatewayFilterChain</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>cloud<span class="token punctuation">.</span>gateway<span class="token punctuation">.</span>filter<span class="token punctuation">.</span></span><span class="token class-name">GlobalFilter</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>core<span class="token punctuation">.</span>io<span class="token punctuation">.</span>buffer<span class="token punctuation">.</span></span><span class="token class-name">DataBuffer</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>http<span class="token punctuation">.</span></span><span class="token class-name">HttpHeaders</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>http<span class="token punctuation">.</span></span><span class="token class-name">HttpStatus</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>http<span class="token punctuation">.</span></span><span class="token class-name">MediaType</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>http<span class="token punctuation">.</span>server<span class="token punctuation">.</span>reactive<span class="token punctuation">.</span></span><span class="token class-name">ServerHttpResponse</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>stereotype<span class="token punctuation">.</span></span><span class="token class-name">Component</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>util<span class="token punctuation">.</span></span><span class="token class-name">StringUtils</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>web<span class="token punctuation">.</span>server<span class="token punctuation">.</span></span><span class="token class-name">ServerWebExchange</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">reactor<span class="token punctuation">.</span>core<span class="token punctuation">.</span>publisher<span class="token punctuation">.</span></span><span class="token class-name">Mono</span></span><span class="token punctuation">;</span>

<span class="token keyword">import</span> <span class="token import"><span class="token namespace">java<span class="token punctuation">.</span>nio<span class="token punctuation">.</span>charset<span class="token punctuation">.</span></span><span class="token class-name">StandardCharsets</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">java<span class="token punctuation">.</span>util<span class="token punctuation">.</span></span><span class="token class-name">List</span></span><span class="token punctuation">;</span>

<span class="token annotation punctuation">@Component</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">AuthGlobalFilter</span>  <span class="token keyword">implements</span> <span class="token class-name">GlobalFilter</span> <span class="token punctuation">{</span>
    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">public</span> <span class="token class-name">Mono</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Void</span><span class="token punctuation">&gt;</span></span> <span class="token function">filter</span><span class="token punctuation">(</span><span class="token class-name">ServerWebExchange</span> exchange<span class="token punctuation">,</span> <span class="token class-name">GatewayFilterChain</span> chain<span class="token punctuation">)</span> <span class="token punctuation">{</span>


        <span class="token class-name">String</span> token <span class="token operator">=</span> exchange<span class="token punctuation">.</span><span class="token function">getRequest</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getQueryParams</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getFirst</span><span class="token punctuation">(</span><span class="token string">&quot;token&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">ServerHttpResponse</span> response <span class="token operator">=</span> exchange<span class="token punctuation">.</span><span class="token function">getResponse</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span><span class="token class-name">StringUtils</span><span class="token punctuation">.</span><span class="token function">hasText</span><span class="token punctuation">(</span>token<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
            <span class="token comment">//没登录</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;没有登录&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            response<span class="token punctuation">.</span><span class="token function">setStatusCode</span><span class="token punctuation">(</span><span class="token class-name">HttpStatus</span><span class="token punctuation">.</span><span class="token constant">UNAUTHORIZED</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token comment">//返回401状态</span>
            <span class="token comment">//return response.setComplete();</span>

            <span class="token comment">//但是一般访问接口应该返回json格式</span>
            <span class="token class-name">String</span> ret <span class="token operator">=</span> <span class="token string">&quot;{\&quot;code\&quot;:401, \&quot;msg\&quot;:\&quot;没有登录\&quot;, \&quot;data\&quot;:\&quot;\&quot;}&quot;</span><span class="token punctuation">;</span>
            <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span> bits <span class="token operator">=</span> ret<span class="token punctuation">.</span><span class="token function">getBytes</span><span class="token punctuation">(</span><span class="token class-name">StandardCharsets</span><span class="token punctuation">.</span><span class="token constant">UTF_8</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token comment">//把字节数据转换成一个DataBuffer</span>
            <span class="token class-name">DataBuffer</span> buffer <span class="token operator">=</span> response<span class="token punctuation">.</span><span class="token function">bufferFactory</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">wrap</span><span class="token punctuation">(</span>bits<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">return</span> response<span class="token punctuation">.</span><span class="token function">writeWith</span><span class="token punctuation">(</span><span class="token class-name">Mono</span><span class="token punctuation">.</span><span class="token function">just</span><span class="token punctuation">(</span>buffer<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>

        <span class="token keyword">return</span> chain<span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">(</span>exchange<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br></div></div><p>注意：如果中文乱码，需要加上响应头</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token class-name">HttpHeaders</span> httpHeaders <span class="token operator">=</span> response<span class="token punctuation">.</span><span class="token function">getHeaders</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//返回数据格式</span>
httpHeaders<span class="token punctuation">.</span><span class="token function">setContentType</span><span class="token punctuation">(</span><span class="token class-name">MediaType</span><span class="token punctuation">.</span><span class="token constant">APPLICATION_JSON</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p><strong>2&gt;启动并测试</strong></p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20201030172557201.png" alt="image-20201030172557201"></strong></p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20201030172635132.png" alt="image-20201030172635132"></strong></p> <h3 id="_8-7-集成sentinel实现网关限流"><a href="#_8-7-集成sentinel实现网关限流" class="header-anchor">#</a> 8.7 集成Sentinel实现网关限流</h3> <p>网关是所有请求的公共入口，所以可以在网关进行限流，而且限流的方式也很多，我们本次采用前</p> <p>面学过的Sentinel组件来实现网关的限流。Sentinel支持对SpringCloud Gateway、Zuul等主流网关进</p> <p>行限流。</p> <p>从1.6.0版本开始，Sentinel提供了SpringCloud Gateway的适配模块，可以提供两种资源维度的限流：</p> <ul><li><p>route维度：即在Spring配置文件中配置的路由条目，资源名为对应的routeId</p></li> <li><p>自定义API维度：用户可以利用Sentinel提供的API来自定义一些API分组</p></li></ul> <h4 id="_8-7-1-网关集成sentinel"><a href="#_8-7-1-网关集成sentinel" class="header-anchor">#</a> 8.7.1 网关集成Sentinel</h4> <p>https://github.com/alibaba/Sentinel/wiki/网关限流</p> <ol><li><p><strong>添加依赖</strong></p> <div class="language-xml line-numbers-mode"><pre class="language-xml"><code><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">&gt;</span></span>
	<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>com.alibaba.csp<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
	<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>sentinel-spring-cloud-gateway-adapter<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>com.alibaba.cloud<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>spring-cloud-starter-alibaba-sentinel<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>com.alibaba.cloud<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>spring-cloud-alibaba-sentinel-gateway<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div></li> <li><p><strong>添加配置</strong></p> <div class="language-yml line-numbers-mode"><pre class="language-yml"><code><span class="token key atrule">spring</span><span class="token punctuation">:</span>
  <span class="token key atrule">cloud</span><span class="token punctuation">:</span>
    <span class="token key atrule">sentinel</span><span class="token punctuation">:</span>
      <span class="token key atrule">transport</span><span class="token punctuation">:</span>
        <span class="token key atrule">port</span><span class="token punctuation">:</span> <span class="token number">8719</span>
        <span class="token key atrule">dashboard</span><span class="token punctuation">:</span> localhost<span class="token punctuation">:</span><span class="token number">8080</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div></li></ol> <h4 id="_8-7-2-api分组"><a href="#_8-7-2-api分组" class="header-anchor">#</a> 8.7.2 API分组</h4> <p>Sentinel中支持按照API分组进行限流,就是我们可以按照特定规则进行限流.</p> <p>在管控台页面中提供了三种方式的API分组管理</p> <ul><li>精准匹配</li> <li>前缀匹配</li> <li>正则匹配</li></ul> <p>以商品服务做测试案例，在shop-product-server服务中定义了如下的接口地址</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@RestController</span>
<span class="token annotation punctuation">@RequestMapping</span><span class="token punctuation">(</span><span class="token string">&quot;/v1&quot;</span><span class="token punctuation">)</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">TestController</span> <span class="token punctuation">{</span>
    <span class="token annotation punctuation">@RequestMapping</span><span class="token punctuation">(</span><span class="token string">&quot;/test1&quot;</span><span class="token punctuation">)</span>
    <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">test1</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token string">&quot;test1&quot;</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token annotation punctuation">@RequestMapping</span><span class="token punctuation">(</span><span class="token string">&quot;/test2&quot;</span><span class="token punctuation">)</span>
    <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">test2</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token string">&quot;test2&quot;</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token annotation punctuation">@RequestMapping</span><span class="token punctuation">(</span><span class="token string">&quot;/test3/test&quot;</span><span class="token punctuation">)</span>
    <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">test3</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token string">&quot;test3&quot;</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br></div></div><p><strong>精准匹配</strong></p> <p>1.在API管理中新建API分组，匹配模式选择精准匹配，匹配串写请求URL地址</p> <p>限制api可以：product-service   也可以使product-serv， 测试时需要跟反问一样，另外注意通过网关访问</p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20220326222004832.png" alt="image-20220326222004832"></strong></p> <p>2.在流控规则中，API类型中选择API分组，然后在API名称中选择我们刚刚定义的V1限流</p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20220326222241887.png" alt="image-20220326222241887"></strong></p> <p>3.此时上面三个请求中，只有<code>/product-service/v1/test1会被限流</code></p> <p><strong>前缀匹配</strong></p> <p>1.在API管理中新建API分组，匹配模式选择前缀匹配，匹配串写请求URL地址</p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20220326222451756.png" alt="image-20220326222451756"></strong></p> <p>此时<code>/product-service/v1/test1</code>和<code>/product-service/v1/test2</code>会被限流</p> <p>注意: 如果路径为/*表示匹配一级路径，如果路径为/**表示多级路径</p> <p><strong>正则匹配</strong></p> <p>1.在API管理中新建API分组，匹配模式选择正则匹配，匹配串写请求URL地址</p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20220326222726247.png" alt="image-20220326222724630"></strong></p> <p>此时所有路径都会被限流</p> <h4 id="_8-7-3-修改限流默认返回格式"><a href="#_8-7-3-修改限流默认返回格式" class="header-anchor">#</a> 8.7.3 修改限流默认返回格式</h4> <ol><li><p>在配置类GatewayConfiguration.java中添加如下配置</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@PostConstruct</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">initBlockHandlers</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token class-name">BlockRequestHandler</span> blockRequestHandler <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">BlockRequestHandler</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token keyword">public</span> <span class="token class-name">Mono</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">ServerResponse</span><span class="token punctuation">&gt;</span></span> <span class="token function">handleRequest</span><span class="token punctuation">(</span><span class="token class-name">ServerWebExchange</span> serverWebExchange<span class="token punctuation">,</span> <span class="token class-name">Throwable</span> throwable<span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token class-name">Map</span> map <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">HashMap</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		map<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">&quot;code&quot;</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		map<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">&quot;message&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;接口被限流了&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token keyword">return</span> <span class="token class-name">ServerResponse</span><span class="token punctuation">.</span><span class="token function">status</span><span class="token punctuation">(</span><span class="token class-name">HttpStatus</span><span class="token punctuation">.</span><span class="token constant">OK</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
				<span class="token function">contentType</span><span class="token punctuation">(</span><span class="token class-name">MediaType</span><span class="token punctuation">.</span><span class="token constant">APPLICATION_JSON</span><span class="token punctuation">)</span><span class="token punctuation">.</span>
				<span class="token function">body</span><span class="token punctuation">(</span><span class="token class-name">BodyInserters</span><span class="token punctuation">.</span><span class="token function">fromValue</span><span class="token punctuation">(</span>map<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
	<span class="token class-name">GatewayCallbackManager</span><span class="token punctuation">.</span><span class="token function">setBlockHandler</span><span class="token punctuation">(</span>blockRequestHandler<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div></li> <li><p>重启并测试</p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20201030180400822.png" alt="image-20201030180400822"></strong></p></li></ol> <h3 id="_8-8-gateway总结"><a href="#_8-8-gateway总结" class="header-anchor">#</a> 8.8 Gateway总结</h3> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/d52a2834349b033b7d76fe836bfc4cd9d439bdad.jpeg" alt="img"></p> <p><strong>Gateway执行流程</strong></p> <ol><li><code>Gateway Client</code> 向 <code>Spring Cloud Gateway</code> 发送请求</li> <li>请求首先会被 <code>HttpWebHandlerAdapter</code> 进行提取组装成网关上下文</li> <li>然后网关的上下文会传递到 <code>DispatcherHandler</code> ，它负责将请求分发给 <code>RoutePredicateHandlerMapping</code></li> <li><code>RoutePredicateHandlerMapping</code> 负责路由查找，并根据路由断言判断路由是否可用</li> <li>如果过断言成功，由<code>FilteringWebHandler</code> 创建过滤器链并调用</li> <li>通过特定于请求的 <code>Fliter</code> 链运行请求，<code>Filter</code> 被虚线分隔的原因是Filter可以在发送代理请求之前（pre）和之后（post）运行逻辑</li> <li>执行所有pre过滤器逻辑。然后进行代理请求。发出代理请求后，将运行“post”过滤器逻辑。</li> <li>处理完毕之后将 <code>Response</code> 返回到 <code>Gateway</code> 客户端</li></ol> <p><strong>Gateway过滤器</strong></p> <ul><li>Filter在pre类型的过滤器可以做参数效验、权限效验、流量监控、日志输出、协议转换等。</li> <li>Filter在post类型的过滤器可以做响应内容、响应头的修改、日志输出、流量监控等</li></ul> <p><strong>核心思想</strong></p> <ul><li>路由转发+执行过滤器链</li></ul> <h2 id="九、链路追踪-sleuth-zipkin"><a href="#九、链路追踪-sleuth-zipkin" class="header-anchor">#</a> 九、链路追踪 Sleuth&amp;Zipkin</h2> <p>微服务架构是一个分布式架构，它按业务划分服务单元，一个分布式系统往往有很多个服务单元。由于服务单元数量众多，业务的复杂性，如果出现了错误和异常，很难去定位。主要体现在，一个请求可能需要调用很多个服务，而内部服务的调用复杂性，决定了问题难以定位。所以微服务架构中，必须实现分布式链路追踪，去跟进<strong>一个请求到底有哪些服务参与，参与的顺序又是怎样的，从而达到每个请求的步骤清晰可见，出了问题，很快定位</strong>。</p> <p>分布式链路追踪（Distributed Tracing），就是将一次分布式请求还原成调用链路，进行日志记录，性能监控并将一次分布式请求的调用情况集中展示。比如各个服务节点上的耗时、请求具体到达哪台机器上、每个服务节点的请求状态等等。</p> <h3 id="_9-1-常见的链路追踪技术"><a href="#_9-1-常见的链路追踪技术" class="header-anchor">#</a> 9.1 常见的链路追踪技术</h3> <ul><li><p><strong>cat</strong> ：由大众点评开源，基于Java开发的实时应用监控平台，包括实时应用监控，业务监控 。 集成方案是通过代码埋点的方式来实现监控，比如： 拦截器，过滤器等。 对代码的侵入性很大，集成成本较高。风险较大。</p></li> <li><p><strong>zipkin</strong> ：由Twitter公司开源，开放源代码分布式的跟踪系统，用于收集服务的定时数据，以解决微服务架构中的延迟问题，包括：数据的收集、存储、查找和展现。该产品结合spring-cloud-sleuth使用较为简单， 集成很方便， 但是功能较简单。</p></li> <li><p><strong>pinpoint</strong>： Pinpoint是韩国人开源的基于字节码注入的调用链分析，以及应用监控分析工具。特点是支持多种插件，UI功能强大，接入端无代码侵入。</p></li> <li><p><strong>skywalking</strong>：SkyWalking是本土开源的基于字节码注入的调用链分析，以及应用监控分析工具。特点是支持多种插件，UI功能较强，接入端无代码侵入。目前已加入Apache孵化器。</p></li> <li><p><strong>Sleuth</strong></p> <p>SpringCloud 提供的分布式系统中链路追踪解决方案。</p></li></ul> <h3 id="_9-2-集成链路追踪组件sleuth"><a href="#_9-2-集成链路追踪组件sleuth" class="header-anchor">#</a> 9.2 集成链路追踪组件Sleuth</h3> <ol><li><p><strong>在product-server和order-server中添加sleuth依赖</strong></p> <div class="language-xml line-numbers-mode"><pre class="language-xml"><code><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">&gt;</span></span>
	<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>org.springframework.cloud<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
	<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>spring-cloud-starter-sleuth<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div></li> <li><p><strong>订单微服务调用商品微服务，这个流程中通过@Slf4j打印日志.重启服务并访问测试</strong></p> <p>订单服务日志结果:</p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20201101203508432.png" alt="image-20201101203506704"></strong></p> <p>商品服务日志结果:</p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20201101203619884.png" alt="image-20201101203619884"></strong></p></li></ol> <h3 id="_9-3-日志参数解释"><a href="#_9-3-日志参数解释" class="header-anchor">#</a> 9.3 日志参数解释</h3> <p>日志格式:
[order-server,c323c72e7009c077,fba72d9c65745e60,false]</p> <p>1、第一个值，spring.application.name的值</p> <p>2、第二个值，c323c72e7009c077 ，sleuth生成的一个ID，叫Trace ID，用来标识一条请求链路，一条请求链路中包含一个Trace ID，多个Span ID</p> <p>3、第三个值，fba72d9c65745e60、spanID 基本的工作单元，获取元数据，如发送一个http</p> <p>4、第四个值：true，是否要将该信息输出到zipkin服务中来收集和展示。</p> <h3 id="_9-4-zipkin-sleuth整合"><a href="#_9-4-zipkin-sleuth整合" class="header-anchor">#</a> 9.4 Zipkin+Sleuth整合</h3> <p>zipkin是Twitter基于google的分布式监控系统Dapper（论文）的开发源实现，zipkin用于跟踪分布式服务之间的应用数据链路，分析处理延时，帮助我们改进系统的性能和定位故障。</p> <p>官网:https://zipkin.io/</p> <ol><li><p><strong>下载Zipkin的jar包，在官网可以下载.</strong></p></li> <li><p><strong>通过命令行，输入下面的命令启动ZipKin Server</strong></p> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code><span class="token function">java</span> <span class="token parameter variable">-jar</span> zipkin-server-2.22.1-exec.jar
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div></li> <li><p>通过浏览器访问 http://localhost:9411访问</p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20201102091554126.png" alt="image-20201102091554126"></strong></p></li> <li><p><strong>在订单微服务和商品微服务中添加zipkin依赖</strong></p> <div class="language-xml line-numbers-mode"><pre class="language-xml"><code><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">&gt;</span></span>
	<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>org.springframework.cloud<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
	<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>spring-cloud-starter-zipkin<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div></li> <li><p><strong>在订单微服务和商品微服务中添加如下配置:</strong></p> <div class="language-yaml line-numbers-mode"><pre class="language-yaml"><code><span class="token key atrule">spring</span><span class="token punctuation">:</span>
  <span class="token key atrule">zipkin</span><span class="token punctuation">:</span>
    <span class="token key atrule">base-url</span><span class="token punctuation">:</span> http<span class="token punctuation">:</span>//127.0.0.1<span class="token punctuation">:</span>9411/ <span class="token comment">#zipkin server的请求地址</span>
    <span class="token key atrule">discoveryClientEnabled</span><span class="token punctuation">:</span> <span class="token boolean important">false</span> <span class="token comment">#让nacos把它当成一个URL，而不要当做服务名</span>
  <span class="token key atrule">sleuth</span><span class="token punctuation">:</span> 
    <span class="token key atrule">sampler</span><span class="token punctuation">:</span> 
      <span class="token key atrule">probability</span><span class="token punctuation">:</span> <span class="token number">1.0</span> <span class="token comment">#采样的百分比</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div></li> <li><p>**重启订单微服务和商品微服务,**访问 http://localhost:8091/save?uid=1&amp;pid=1</p></li> <li><p><strong>访问zipkin的UI界面，观察效果</strong></p></li></ol> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20201102092213549.png" alt="image-20201102092213549"></strong></p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20201102092245887.png" alt="image-20201102092245887"></strong></p> <h2 id="十、配置中心-nacos-config"><a href="#十、配置中心-nacos-config" class="header-anchor">#</a> 十、配置中心 Nacos Config</h2> <h3 id="_10-1-服务配置中心介绍"><a href="#_10-1-服务配置中心介绍" class="header-anchor">#</a> 10.1 服务配置中心介绍</h3> <p>首先我们来看一下,微服务架构下关于配置文件的一些问题：</p> <ol><li>配置文件相对分散。在一个微服务架构下，配置文件会随着微服务的增多变的越来越多，而且分散</li></ol> <p>在各个微服务中，不好统一配置和管理。</p> <ol start="2"><li>配置文件无法区分环境。微服务项目可能会有多个环境，例如：测试环境、预发布环境、生产环</li></ol> <p>境。每一个环境所使用的配置理论上都是不同的，一旦需要修改，就需要我们去各个微服务下手动</p> <p>维护，这比较困难。</p> <ol start="3"><li>配置文件无法实时更新。我们修改了配置文件之后，必须重新启动微服务才能使配置生效，这对一</li></ol> <p>个正在运行的项目来说是非常不友好的。</p> <p>基于上面这些问题，我们就需要<strong>配置中心</strong>的加入来解决这些问题。</p> <p><strong>配置中心的思路是</strong>：</p> <p>首先把项目中各种配置全部都放到一个集中的地方进行统一管理，并提供一套标准的接口。</p> <p>当各个服务需要获取配置的时候，就来配置中心的接口拉取自己的配置。</p> <p>当配置中心中的各种参数有更新的时候，也能通知到各个服务实时的过来同步最新的信息，使之动</p> <p>态更新。</p> <p>当加入了服务配置中心之后，我们的系统架构图会变成下面这样：</p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20201101222022206.png" alt="image-20201101222022206"></strong></p> <h3 id="_10-2-常见的服务配置中心"><a href="#_10-2-常见的服务配置中心" class="header-anchor">#</a> 10.2 常见的服务配置中心</h3> <ul><li><strong>Apollo</strong></li></ul> <p>Apollo是由携程开源的分布式配置中心。特点有很多，比如：配置更新之后可以实时生效，支持灰</p> <p>度发布功能，并且能对所有的配置进行版本管理、操作审计等功能，提供开放平台API。并且资料</p> <p>也写的很详细。</p> <ul><li><strong>Disconf</strong></li></ul> <p>Disconf是由百度开源的分布式配置中心。它是基于Zookeeper来实现配置变更后实时通知和生效</p> <p>的。</p> <ul><li><strong>SpringCloud Config</strong></li></ul> <p>这是Spring Cloud中带的配置中心组件。它和Spring是无缝集成，使用起来非常方便，并且它的配</p> <p>置存储支持Git。不过它没有可视化的操作界面，配置的生效也不是实时的，需要重启或去刷新。</p> <ul><li><strong>Nacos</strong></li></ul> <p>这是SpingCloud alibaba技术栈中的一个组件，前面我们已经使用它做过服务注册中心。其实它也</p> <p>集成了服务配置的功能，我们可以直接使用它作为服务配置中心。</p> <h3 id="_10-3-nacos-config入门"><a href="#_10-3-nacos-config入门" class="header-anchor">#</a> 10.3 Nacos Config入门</h3> <p>使用nacos作为配置中心，其实就是将nacos当做一个服务端，将各个微服务看成是客户端，我们将各个微服务的配置文件统一存放在nacos上，然后各个微服务从nacos上拉取配置即可。接下来我们以商品微服务为例，学习nacos config的使用。</p> <ol><li><p><strong>搭建nacos环境【使用现有的nacos环境即可】</strong></p></li> <li><p><strong>在商品微服务中引入nacos的依赖</strong></p> <div class="language-xml line-numbers-mode"><pre class="language-xml"><code><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">&gt;</span></span>
	<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>com.alibaba.cloud<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
	<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>spring-cloud-starter-alibaba-nacos-config<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span> 
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div></li> <li><p><strong>在微服务中添加nacos config的配置</strong></p> <p><strong>注意</strong>:不能使用原来的<code>application.yml</code>作为配置文件，而是新建一个<code>bootstrap.yml</code>作为配置文件</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>配置文件优先级(由高到低):
bootstrap.properties -&gt; bootstrap.yml -&gt; application.properties -&gt; application.yml
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><div class="language-yaml line-numbers-mode"><pre class="language-yaml"><code><span class="token key atrule">spring</span><span class="token punctuation">:</span>
  <span class="token key atrule">application</span><span class="token punctuation">:</span>
    <span class="token key atrule">name</span><span class="token punctuation">:</span> product<span class="token punctuation">-</span>service
  <span class="token key atrule">cloud</span><span class="token punctuation">:</span>
    <span class="token key atrule">nacos</span><span class="token punctuation">:</span>
      <span class="token key atrule">config</span><span class="token punctuation">:</span>
        <span class="token key atrule">server-addr</span><span class="token punctuation">:</span> 127.0.0.1<span class="token punctuation">:</span><span class="token number">8848</span> <span class="token comment">#nacos中心地址</span>
        <span class="token key atrule">file-extension</span><span class="token punctuation">:</span> yaml <span class="token comment">## 配置文件格式</span>
  <span class="token key atrule">profiles</span><span class="token punctuation">:</span> 
  	<span class="token key atrule">active</span><span class="token punctuation">:</span> dev <span class="token comment">## 环境标识</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div></li> <li><p><strong>在nacos中添加配置,然后把商品微服务application.yml配置复制到配置内容中.</strong></p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20201102093026806.png" alt="image-20201102093026806"></strong></p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20201102095324857.png" alt="image-20201102095324857"></p></li> <li><p><strong>注释本地的application.yam中的内容， 启动程序进行测试</strong></p></li> <li><p><strong>如果依旧可以成功访问程序，说明我们nacos的配置中心功能已经实现</strong></p></li></ol> <p><strong>操作原理：</strong></p> <p>观察启动后打印的日志，大体能看出操作原理</p> <p>1&gt;项目启动，先加载bootstrap.yml解析里面配置，得到3个核心信息</p> <ul><li>服务名：product-service</li> <li>nacos配置中心地址：127.0.0.1:8848</li> <li>激活环境：dev</li> <li>文件后缀: yaml</li></ul> <p>2&gt;访nacos配置中心找项目的配置文件</p> <p>没有指定具体分组默认：DEFAULT_GROUP</p> <p>组 + 服务名 + 激活换---&gt;DEFAULT_GROUP--product-service-dev.yaml 完全可以找到配置文件</p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20221020115744535.png" alt="image-20221020115744535"></p> <h3 id="_10-4-配置动态刷新"><a href="#_10-4-配置动态刷新" class="header-anchor">#</a> 10.4 配置动态刷新</h3> <p>所谓的动态刷新：项目运行中，手动改了nacos中的配置，项目在不停机的情况下也可以读到最新的数据。</p> <p>操作步骤：</p> <p><strong>1&gt;在nacos中的product-service-dev.yaml配置项中添加下面配置:</strong></p> <div class="language-yaml line-numbers-mode"><pre class="language-yaml"><code><span class="token key atrule">appConfig</span><span class="token punctuation">:</span>
  <span class="token key atrule">name</span><span class="token punctuation">:</span> product2022
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p><strong>2&gt;在商品微服务中新增NacosConfigControlller.java</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@RestController</span>
<span class="token annotation punctuation">@RefreshScope</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">NacosConfigController</span> <span class="token punctuation">{</span>
    <span class="token annotation punctuation">@Value</span><span class="token punctuation">(</span><span class="token string">&quot;${appConfig.name}&quot;</span><span class="token punctuation">)</span>
    <span class="token keyword">private</span> <span class="token class-name">String</span> appConfigName<span class="token punctuation">;</span>
    <span class="token annotation punctuation">@RequestMapping</span><span class="token punctuation">(</span><span class="token string">&quot;/nacosConfig&quot;</span><span class="token punctuation">)</span>
    <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">nacosConfig</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token string">&quot;远程信息:&quot;</span><span class="token operator">+</span>appConfigName<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><p>注意：</p> <p>必须贴上@RefreshScope 在类上才有效果</p> <p>**@RefreshScope 操作原理：**当配置发生更新时，贴有@RefreshScope注解的类会重新IOC创建新的对象，该类中加载的数据重新更新。</p> <h3 id="_10-5-配置共享"><a href="#_10-5-配置共享" class="header-anchor">#</a> 10.5 配置共享</h3> <h4 id="_10-5-1-配置环境"><a href="#_10-5-1-配置环境" class="header-anchor">#</a> 10.5.1 配置环境</h4> <p>真实项目开发一般会有3套部署环境，</p> <p>**开发：**dev，配置是开发的环境，比如开发环境数据库四要素，Redis环境等。</p> <p>**测试：**test，配置是测试的环境，比如测试环境数据库四要素，Redis环境等。</p> <p>**生产：**prod，配置是生产的环境，比如生产环境数据库四要素，Redis环境等。</p> <p>所以在项目开发时，必须编写3个配置环境文件</p> <div class="language-properties line-numbers-mode"><pre class="language-properties"><code><span class="token key attr-name">appliction-dev.yaml</span> <span class="token value attr-value"> #开发</span>
<span class="token key attr-name">appliction-test.yaml</span> <span class="token value attr-value">#测试</span>
<span class="token key attr-name">appliction-prod.yaml</span> <span class="token value attr-value">#生产</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>同时项目启动前需要明确指定要激活环境</p> <div class="language-yaml line-numbers-mode"><pre class="language-yaml"><code><span class="token key atrule">spring</span><span class="token punctuation">:</span>
  <span class="token key atrule">profiles</span><span class="token punctuation">:</span>
    <span class="token key atrule">active</span><span class="token punctuation">:</span> dev <span class="token comment">## 环境标识</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>按这个标准，上面的案例还需要在nacos中创建2个配置文件</p> <div class="language-properties line-numbers-mode"><pre class="language-properties"><code>product-service-test.yaml
product-service-prod.yaml
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>nacos非常贴心，提供克隆功能</p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20221020163613835.png" alt="image-20221020163613835"></p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20221020163638293.png" alt="image-20221020163638293">*</p> <h4 id="_10-5-2-环境配置文件共享"><a href="#_10-5-2-环境配置文件共享" class="header-anchor">#</a> 10.5.2 环境配置文件共享</h4> <p>当环境多了，那对应的配置也会越来越多，这时你会发现有很多配置是重复的，此时就要考虑可不可以将相同配置提取出来，放置到一个共有文件中，然后实现共享呢？答案是Yes</p> <p><strong>第一种场景：同一个微服务配置文件共享</strong></p> <p>操作上非常简单，只需要写一个以：spring.application.name 命名的配置文件即可。</p> <p>比如：</p> <p>application-dev.yaml ,application-test.yaml   ,application-prod.yaml</p> <p>提取公共文件名：<strong>application.yaml</strong>     看到没，没有后缀即可。</p> <p>回到nacos的案例，只需要配置多一个文件：<strong>product-service.yaml</strong> 就可以啦。</p> <p><strong>案例</strong></p> <p>将之前的配置删掉：product-service-test.yaml  product-service-prod.yaml</p> <div class="language-yaml line-numbers-mode"><pre class="language-yaml"><code><span class="token key atrule">appConfig</span><span class="token punctuation">:</span>
  <span class="token key atrule">name</span><span class="token punctuation">:</span> product2022
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>项目启动之后，报错</p> <div class="language-yaml line-numbers-mode"><pre class="language-yaml"><code><span class="token key atrule">spring</span><span class="token punctuation">:</span>
  <span class="token key atrule">profiles</span><span class="token punctuation">:</span>
    <span class="token key atrule">active</span><span class="token punctuation">:</span> test <span class="token comment">## 环境标识</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>切换到test环境也报错，此时只需要额外添加一个配置文件：<strong>product-service.yaml</strong></p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20221020165332613.png" alt="image-20221020165332613">*</p> <p><strong>第二种场景：不同微服务中间配置文件共享</strong></p> <p>不同为服务之间也可以实现配置共享，原理有点类似于文件引入，就是定义一个公共配置文件，然后在当前配置文件中引入。</p> <p><strong>操作步骤：</strong></p> <p>1&gt;在nacos中定义一个DataID为global-config.yaml的配置，用于所有微服务共享</p> <div class="language-yaml line-numbers-mode"><pre class="language-yaml"><code><span class="token key atrule">globalConfig</span><span class="token punctuation">:</span> global
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/image-20221020171024737.png" alt="image-20221020171024737"></strong></p> <p>2&gt;修改bootstrap.yaml</p> <div class="language-yaml line-numbers-mode"><pre class="language-yaml"><code><span class="token key atrule">spring</span><span class="token punctuation">:</span>
  <span class="token key atrule">application</span><span class="token punctuation">:</span>
    <span class="token key atrule">name</span><span class="token punctuation">:</span> product<span class="token punctuation">-</span>service
  <span class="token key atrule">cloud</span><span class="token punctuation">:</span>
    <span class="token key atrule">nacos</span><span class="token punctuation">:</span>
      <span class="token key atrule">config</span><span class="token punctuation">:</span>
        <span class="token key atrule">server-addr</span><span class="token punctuation">:</span> 127.0.0.1<span class="token punctuation">:</span><span class="token number">8848</span> <span class="token comment">#nacos中心地址</span>
        <span class="token key atrule">file-extension</span><span class="token punctuation">:</span> yaml <span class="token comment">## 配置文件格式</span>
        <span class="token key atrule">shared-configs</span><span class="token punctuation">:</span>
          <span class="token punctuation">-</span> <span class="token key atrule">data-id</span><span class="token punctuation">:</span> global<span class="token punctuation">-</span>config.yaml <span class="token comment">## 配置要引入的配置</span>
            <span class="token key atrule">refresh</span><span class="token punctuation">:</span> <span class="token boolean important">true</span>
  <span class="token key atrule">profiles</span><span class="token punctuation">:</span>
    <span class="token key atrule">active</span><span class="token punctuation">:</span> test <span class="token comment">## 环境标识</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br></div></div><p>2&gt;在NacosConfigController.java中新增一个方法</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@RestController</span>
<span class="token annotation punctuation">@RefreshScope</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">NacosConfigController</span> <span class="token punctuation">{</span>
    <span class="token annotation punctuation">@Value</span><span class="token punctuation">(</span><span class="token string">&quot;${appConfig.name}&quot;</span><span class="token punctuation">)</span>
    <span class="token keyword">private</span> <span class="token class-name">String</span> appConfigName<span class="token punctuation">;</span>
    <span class="token annotation punctuation">@Value</span><span class="token punctuation">(</span><span class="token string">&quot;${globalConfig}&quot;</span><span class="token punctuation">)</span>
    <span class="token keyword">private</span> <span class="token class-name">String</span> globalConfig<span class="token punctuation">;</span>
    
    <span class="token annotation punctuation">@RequestMapping</span><span class="token punctuation">(</span><span class="token string">&quot;/nacosConfig&quot;</span><span class="token punctuation">)</span>
    <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">nacosConfig</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token string">&quot;远程信息:&quot;</span><span class="token operator">+</span>appConfigName<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token annotation punctuation">@RequestMapping</span><span class="token punctuation">(</span><span class="token string">&quot;/nacosConfig2&quot;</span><span class="token punctuation">)</span>
    <span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">nacosConfig2</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token string">&quot;全局配置:&quot;</span><span class="token operator">+</span>globalConfig<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br></div></div><p>3&gt;重启服务并测试.</p> <h2 id="十一、小结与作业"><a href="#十一、小结与作业" class="header-anchor">#</a> 十一、小结与作业</h2> <ul><li>微服务概念---开口讲</li> <li>项目架构演变--开口讲</li> <li>微服务项目搭建---建议3遍</li> <li>微服务注册中心-nacos --开口讲10分钟</li> <li>负载均衡组件-ribbon    --开口讲10分钟</li> <li>feign接口引入</li> <li>feign实现原理---实现方案---定义注意要点</li> <li>服务雪崩概念了解---容错方案---必须开口讲</li> <li>sentinel 基本操作---流控--降级--热点--授权--系统</li> <li>sentinel 异常返回定制---@sentinelresource注解使用</li> <li>Feign整合Sentinel</li> <li>gateway  概念,路由转发, 过滤器---建议10分钟</li> <li>gateway 过滤器---局部--全局</li> <li>gateway 跟sentinel整合</li> <li>链路追踪器--sleuth/zipkin----skywalking</li> <li>配置中心</li></ul></div></div>  <div class="page-edit"><!----> <div class="tags"><a href="/tags/?tag=SpringCloudAlibaba" title="标签">#SpringCloudAlibaba</a><a href="/tags/?tag=Nacos" title="标签">#Nacos</a><a href="/tags/?tag=Ribbon" title="标签">#Ribbon</a><a href="/tags/?tag=Feign" title="标签">#Feign</a><a href="/tags/?tag=Sentinel" title="标签">#Sentinel</a><a href="/tags/?tag=Config" title="标签">#Config</a><a href="/tags/?tag=Sleuth" title="标签">#Sleuth</a><a href="/tags/?tag=Zipkin" title="标签">#Zipkin</a></div> <!----></div> <div class="page-nav-wapper"><div class="page-nav-centre-wrap"><a href="/pages/25c0d9/" class="page-nav-centre page-nav-centre-prev"><div class="tooltip">ElasticSearch</div></a> <a href="/pages/0a481a/" class="page-nav-centre page-nav-centre-next"><div class="tooltip">分布式事务与Seata落地</div></a></div> <div class="page-nav"><p class="inner"><span class="prev">
        ←
        <a href="/pages/25c0d9/" class="prev">ElasticSearch</a></span> <span class="next"><a href="/pages/0a481a/">分布式事务与Seata落地</a>→
      </span></p></div></div></div> <div class="article-list"><div class="article-title"><a href="/archives/" class="iconfont icon-bi">最近更新</a></div> <div class="article-wrapper"><dl><dd>01</dd> <dt><a href="/pages/0c07b2/"><div>
            IT杂货铺
            <!----></div></a> <span class="date">03-23</span></dt></dl><dl><dd>02</dd> <dt><a href="/pages/705b35/"><div>
            人事百问
            <!----></div></a> <span class="date">03-22</span></dt></dl><dl><dd>03</dd> <dt><a href="/pages/78eb56/"><div>
            MyBatis-Plus
            <!----></div></a> <span class="date">03-12</span></dt></dl> <dl><dd></dd> <dt><a href="/archives/" class="more">更多文章&gt;</a></dt></dl></div></div></main></div> <div class="footer"><div class="icons"><a href="mailto:langfeiyes@163.com" title="发邮件" target="_blank" class="iconfont icon-youjian"></a><a href="https://github.com/langfeiyes" title="GitHub" target="_blank" class="iconfont icon-github"></a><a href="https://music.163.com/#/playlist?id=755597173" title="听音乐" target="_blank" class="iconfont icon-erji"></a></div> 
  Theme by
  <a href="https://github.com/xugaoyi/vuepress-theme-vdoing" target="_blank" title="本站主题">Vdoing</a> 
    | Copyright © 2024-2024
    <span><a href="https://beian.miit.gov.cn/" target="_blank">粤ICP备18007927号-1</a></span></div> <div class="buttons"><div title="返回顶部" class="button blur go-to-top iconfont icon-fanhuidingbu" style="display:none;"></div> <div title="去评论" class="button blur go-to-comment iconfont icon-pinglun" style="display:none;"></div> <div title="主题模式" class="button blur theme-mode-but iconfont icon-zhuti"><ul class="select-box" style="display:none;"><li class="iconfont icon-zidong">
          跟随系统
        </li><li class="iconfont icon-rijianmoshi">
          浅色模式
        </li><li class="iconfont icon-yejianmoshi">
          深色模式
        </li><li class="iconfont icon-yuedu">
          阅读模式
        </li></ul></div></div> <!----> <!----> <!----></div><div class="global-ui"><div></div></div></div>
    <script src="/assets/js/app.a2a00aa5.js" defer></script><script src="/assets/js/2.256f807a.js" defer></script><script src="/assets/js/63.f849fe75.js" defer></script>
  </body>
</html>
